Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 25ba5a3

Browse files
committed
commit solution 191
1 parent 27c6356 commit 25ba5a3

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

‎index-tags.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
148148
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
149149
| [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> ||
150151
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
151152

152153
#### ****

‎index-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
| [183](https://leetcode-cn.com/problems/customers-who-never-order) | [从不订购的客户](/solution/100-199/0183.customers-who-never-order/) | | <font color=green>简单</font> |
5959
| [189](https://leetcode-cn.com/problems/rotate-array) | [旋转数组](/solution/100-199/0189.rotate-array/) | `数组` | <font color=green>简单</font> ||
6060
| [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> ||
6262
| [193](https://leetcode-cn.com/problems/valid-phone-numbers) | [有效电话号码](/solution/100-199/0193.valid-phone-numbers/) | | <font color=green>简单</font> |
6363
| [195](https://leetcode-cn.com/problems/tenth-line) | [第十行](/solution/100-199/0195.tenth-line/) | | <font color=green>简单</font> |
6464
| [196](https://leetcode-cn.com/problems/delete-duplicate-emails) | [删除重复的电子邮箱](/solution/100-199/0196.delete-duplicate-emails/) | | <font color=green>简单</font> |
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# [191. 位1的个数](https://leetcode-cn.com/problems/number-of-1-bits/description/)
2+
3+
### 题目描述
4+
5+
<p>编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 &lsquo;1&rsquo;&nbsp;的个数(也被称为<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>&nbsp;</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>&nbsp;中,共有三位为 &#39;1&#39;。</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>&nbsp;中,共有一位为 &#39;1&#39;
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 位为 &#39;1&#39;。</pre>
28+
29+
<p>&nbsp;</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>记法来表示有符号整数。因此,在上面的&nbsp;<strong>示例 3</strong>&nbsp;中,输入表示有符号整数 <code>-3</code>。</li>
36+
</ul>
37+
38+
<p>&nbsp;</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+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /