File tree Expand file tree Collapse file tree 5 files changed +102
-1
lines changed
solution/100-199/0191.number-of-1-bits Expand file tree Collapse file tree 5 files changed +102
-1
lines changed Original file line number Diff line number Diff line change 147
147
| [ 136] ( https://leetcode-cn.com/problems/single-number ) | [ 只出现一次的数字] ( /solution/100-199/0136.single-number/ ) | ` 位运算 ` ,` 哈希表 ` | <font color =green >简单</font > | ✅ |
148
148
| [ 169] ( https://leetcode-cn.com/problems/majority-element ) | [ 多数元素] ( /solution/100-199/0169.majority-element/ ) | ` 位运算 ` ,` 数组 ` ,` 分治算法 ` | <font color =green >简单</font > | ✅ |
149
149
| [ 187] ( https://leetcode-cn.com/problems/repeated-dna-sequences ) | [ 重复的dna序列] ( /solution/100-199/0187.repeated-dna-sequences/ ) | ` 位运算 ` ,` 哈希表 ` | <font color =blue >中等</font > | ✅ |
150
+ | [ 191] ( https://leetcode-cn.com/problems/number-of-1-bits ) | [ 位1的个数] ( /solution/100-199/0191.number-of-1-bits/ ) | ` 位运算 ` | <font color =green >简单</font > | ✅ |
150
151
| [ 268] ( https://leetcode-cn.com/problems/missing-number ) | [ 缺失数字] ( /solution/200-299/0268.missing-number/ ) | ` 位运算 ` ,` 数组 ` ,` 数学 ` | <font color =green >简单</font > | ✅ |
151
152
152
153
#### ** 树**
Original file line number Diff line number Diff line change 58
58
| [ 183] ( https://leetcode-cn.com/problems/customers-who-never-order ) | [ 从不订购的客户] ( /solution/100-199/0183.customers-who-never-order/ ) | | <font color =green >简单</font > |
59
59
| [ 189] ( https://leetcode-cn.com/problems/rotate-array ) | [ 旋转数组] ( /solution/100-199/0189.rotate-array/ ) | ` 数组 ` | <font color =green >简单</font > | ✅ |
60
60
| [ 190] ( https://leetcode-cn.com/problems/reverse-bits ) | [ 颠倒二进制位] ( /solution/100-199/0190.reverse-bits/ ) | ` 位运算 ` | <font color =green >简单</font > |
61
- | [ 191] ( https://leetcode-cn.com/problems/number-of-1-bits ) | [ 位1的个数] ( /solution/100-199/0191.number-of-1-bits/ ) | ` 位运算 ` | <font color =green >简单</font > |
61
+ | [ 191] ( https://leetcode-cn.com/problems/number-of-1-bits ) | [ 位1的个数] ( /solution/100-199/0191.number-of-1-bits/ ) | ` 位运算 ` | <font color =green >简单</font > | ✅ |
62
62
| [ 193] ( https://leetcode-cn.com/problems/valid-phone-numbers ) | [ 有效电话号码] ( /solution/100-199/0193.valid-phone-numbers/ ) | | <font color =green >简单</font > |
63
63
| [ 195] ( https://leetcode-cn.com/problems/tenth-line ) | [ 第十行] ( /solution/100-199/0195.tenth-line/ ) | | <font color =green >简单</font > |
64
64
| [ 196] ( https://leetcode-cn.com/problems/delete-duplicate-emails ) | [ 删除重复的电子邮箱] ( /solution/100-199/0196.delete-duplicate-emails/ ) | | <font color =green >简单</font > |
Original file line number Diff line number Diff line change
1
+ # [ 191. 位1的个数] ( https://leetcode-cn.com/problems/number-of-1-bits/description/ )
2
+
3
+ ### 题目描述
4
+
5
+ <p >编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 &lsquo ; 1&rsquo ;  ; 的个数(也被称为<a href =" https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F " target =" _blank " >汉明重量</a >)。</p >
6
+
7
+ <p >  ; </p >
8
+
9
+ <p ><strong >示例 1:</strong ></p >
10
+
11
+ <pre ><strong >输入:</strong >00000000000000000000000000001011
12
+ <strong >输出:</strong >3
13
+ <strong >解释:</strong >输入的二进制串 <code ><strong >00000000000000000000000000001011</strong >  ; 中,共有三位为 ' ; 1' ; 。</code >
14
+ </pre >
15
+
16
+ <p ><strong >示例 2:</strong ></p >
17
+
18
+ <pre ><strong >输入:</strong >00000000000000000000000010000000
19
+ <strong >输出:</strong >1
20
+ <strong >解释:</strong >输入的二进制串 <strong >00000000000000000000000010000000</strong >  ; 中,共有一位为 ' ; 1' ; 。
21
+ </pre >
22
+
23
+ <p ><strong >示例 3:</strong ></p >
24
+
25
+ <pre ><strong >输入:</strong >11111111111111111111111111111101
26
+ <strong >输出:</strong >31
27
+ <strong >解释:</strong >输入的二进制串 <strong >11111111111111111111111111111101</strong > 中,共有 31 位为 ' ; 1' ; 。</pre >
28
+
29
+ <p >  ; </p >
30
+
31
+ <p ><strong >提示:</strong ></p >
32
+
33
+ <ul >
34
+ <li>请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。</li>
35
+ <li>在 Java 中,编译器使用<a href="https://baike.baidu.com/item/二进制补码/5295284" target="_blank">二进制补码</a>记法来表示有符号整数。因此,在上面的 <strong>示例 3</strong> 中,输入表示有符号整数 <code>-3</code>。</li>
36
+ </ul >
37
+
38
+ <p >  ; </p >
39
+
40
+ <p ><strong >进阶</strong >:<br >
41
+ 如果多次调用这个函数,你将如何优化你的算法?</p >
42
+
43
+ ### 解题思路
44
+
45
+ 1 . num & (num - 1) 作用是消除数字 n 的二进制表示中的最后一个 1
46
+
47
+ ![ ] ( http://lc-photo.xwlin.com/191.png )
48
+
49
+ ### 具体解法
50
+
51
+
52
+ #### ** Golang**
53
+ ``` go
54
+ func hammingWeight (num uint32 ) int {
55
+ res := 0
56
+ for num != 0 {
57
+ num = num & (num - 1 )
58
+ res++
59
+ }
60
+ return res
61
+ }
62
+ ```
63
+
64
+
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ /*
4
+ * @lc app=leetcode.cn id=191 lang=golang
5
+ *
6
+ * [191] 位1的个数
7
+ */
8
+
9
+ // @lc code=start
10
+ func hammingWeight (num uint32 ) int {
11
+ res := 0
12
+ for num != 0 {
13
+ num = num & (num - 1 )
14
+ res ++
15
+ }
16
+ return res
17
+ }
18
+
19
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ import (
4
+ "testing"
5
+ )
6
+
7
+ func TestHammingWeight (t * testing.T ) {
8
+ var num uint32
9
+ var ret int
10
+
11
+ num = 00000000000000000000000000001011
12
+ ret = 3
13
+
14
+ if ret != hammingWeight (num ) {
15
+ t .Fatalf ("case fails %v\n " , ret )
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments