Skip to content

Commit 1a2da52

Browse files
committed
[E:46/531, M:52/969, H:6/386] add No: 191 Number of 1 Bits
1 parent 4333265 commit 1a2da52

File tree

9 files changed

+279
-1
lines changed

9 files changed

+279
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## [位1的个数](https://leetcode-cn.com/problems/number-of-1-bits/)
2+
3+
编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为[汉明重量](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F))。
4+
5+
 
6+
7+
**提示:**
8+
9+
* 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
10+
* 在 Java 中,编译器使用[二进制补码](https://baike.baidu.com/item/二进制补码/5295284)记法来表示有符号整数。因此,在上面的 **示例 3** 中,输入表示有符号整数 `-3`
11+
12+
 
13+
14+
**进阶**
15+
16+
* 如果多次调用这个函数,你将如何优化你的算法?
17+
18+
 
19+
20+
**示例 1:**
21+
22+
`
23+
**输入:**00000000000000000000000000001011
24+
**输出:**3
25+
**解释:**输入的二进制串 `**00000000000000000000000000001011** 中,共有三位为 '1'。`
26+
`
27+
28+
**示例 2:**
29+
30+
`
31+
**输入:**00000000000000000000000010000000
32+
**输出:**1
33+
**解释:**输入的二进制串 **00000000000000000000000010000000** 中,共有一位为 '1'。
34+
`
35+
36+
**示例 3:**
37+
38+
`
39+
**输入:**11111111111111111111111111111101
40+
**输出:**31
41+
**解释:**输入的二进制串 **11111111111111111111111111111101** 中,共有 31 位为 '1'。`
42+
43+
 
44+
45+
**提示:**
46+
47+
* 输入必须是长度为 `32`**二进制串**
48+
49+
 
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/191/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input uint32
18+
want int
19+
}{
20+
{
21+
name: "test-11",
22+
input: 11,
23+
want: 3,
24+
},
25+
{
26+
name: "test-128",
27+
input: 128,
28+
want: 1,
29+
},
30+
{
31+
name: "test-4294967293",
32+
input: 4294967293,
33+
want: 31,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
45+
solution.Export(test.input)
46+
cancel()
47+
})
48+
49+
if timeout {
50+
testLog.Fail(idx+1, test.name, "timeout")
51+
continue
52+
}
53+
54+
got := solution.Export(test.input)
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(num uint32) int {
4+
return hammingWeight(num)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingWeight(num uint32) int {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(num uint32) int {
4+
return hammingWeight(num)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingWeight(num uint32) int {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## [位1的个数](https://leetcode-cn.com/problems/number-of-1-bits/)
2+
3+
编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为[汉明重量](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F))。
4+
5+
 
6+
7+
**提示:**
8+
9+
* 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
10+
* 在 Java 中,编译器使用[二进制补码](https://baike.baidu.com/item/二进制补码/5295284)记法来表示有符号整数。因此,在上面的 **示例 3** 中,输入表示有符号整数 `-3`
11+
12+
 
13+
14+
**进阶**
15+
16+
* 如果多次调用这个函数,你将如何优化你的算法?
17+
18+
 
19+
20+
**示例 1:**
21+
22+
`
23+
**输入:**00000000000000000000000000001011
24+
**输出:**3
25+
**解释:**输入的二进制串 `**00000000000000000000000000001011** 中,共有三位为 '1'。`
26+
`
27+
28+
**示例 2:**
29+
30+
`
31+
**输入:**00000000000000000000000010000000
32+
**输出:**1
33+
**解释:**输入的二进制串 **00000000000000000000000010000000** 中,共有一位为 '1'。
34+
`
35+
36+
**示例 3:**
37+
38+
`
39+
**输入:**11111111111111111111111111111101
40+
**输出:**31
41+
**解释:**输入的二进制串 **11111111111111111111111111111101** 中,共有 31 位为 '1'。`
42+
43+
 
44+
45+
**提示:**
46+
47+
* 输入必须是长度为 `32`**二进制串**
48+
49+
 
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/191/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input uint32
18+
want int
19+
}{
20+
{
21+
name: "test-11",
22+
input: 11,
23+
want: 3,
24+
},
25+
{
26+
name: "test-128",
27+
input: 128,
28+
want: 1,
29+
},
30+
{
31+
name: "test-4294967293",
32+
input: 4294967293,
33+
want: 31,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
45+
solution.Export(test.input)
46+
cancel()
47+
})
48+
49+
if timeout {
50+
testLog.Fail(idx+1, test.name, "timeout")
51+
continue
52+
}
53+
54+
got := solution.Export(test.input)
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(num uint32) int {
4+
return hammingWeight(num)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingWeight(num uint32) int {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(num uint32) int {
4+
return hammingWeight(num)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingWeight(num uint32) int {
13+
14+
}

0 commit comments

Comments
 (0)