剑指 Offer 15:二进制中1的个数
This commit is contained in:
parent
24ed143e35
commit
947bac1e95
@ -0,0 +1,62 @@
|
|||||||
|
//请实现一个函数,输入一个整数(以二进制串形式),输出该数二进制表示中 1 的个数。例如,把 9 表示成二进制是 1001,有 2 位是 1。因此,如果输入
|
||||||
|
//9,则该函数输出 2。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:00000000000000000000000000001011
|
||||||
|
//输出:3
|
||||||
|
//解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:00000000000000000000000010000000
|
||||||
|
//输出:1
|
||||||
|
//解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:11111111111111111111111111111101
|
||||||
|
//输出:31
|
||||||
|
//解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 输入必须是长度为 32 的 二进制串 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 注意:本题与主站 191 题相同:https://leetcode-cn.com/problems/number-of-1-bits/
|
||||||
|
// Related Topics 位运算
|
||||||
|
// 👍 139 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//剑指 Offer 15:二进制中1的个数
|
||||||
|
public class ErJinZhiZhong1deGeShuLcof {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ErJinZhiZhong1deGeShuLcof().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
public class Solution {
|
||||||
|
// you need to treat n as an unsigned value
|
||||||
|
public int hammingWeight(int n) {
|
||||||
|
return Integer.bitCount(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<p>请实现一个函数,输入一个整数(以二进制串形式),输出该数二进制表示中 1 的个数。例如,把 9 表示成二进制是 1001,有 2 位是 1。因此,如果输入 9,则该函数输出 2。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>00000000000000000000000000001011
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
<strong>解释:</strong>输入的二进制串 <code><strong>00000000000000000000000000001011</strong> 中,共有三位为 '1'。</code>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>00000000000000000000000010000000
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
<strong>解释:</strong>输入的二进制串 <strong>00000000000000000000000010000000</strong> 中,共有一位为 '1'。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>11111111111111111111111111111101
|
||||||
|
<strong>输出:</strong>31
|
||||||
|
<strong>解释:</strong>输入的二进制串 <strong>11111111111111111111111111111101</strong> 中,共有 31 位为 '1'。</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>输入必须是长度为 <code>32</code> 的 <strong>二进制串</strong> 。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p>注意:本题与主站 191 题相同:<a href="https://leetcode-cn.com/problems/number-of-1-bits/">https://leetcode-cn.com/problems/number-of-1-bits/</a></p>
|
||||||
|
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 139</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user