Skip to content

Commit 0ccf8be

Browse files
committed
[E:56/531, M:52/969, H:6/386] add No: 461 Hamming Distance
1 parent 51ac836 commit 0ccf8be

File tree

9 files changed

+217
-1
lines changed

9 files changed

+217
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## [汉明距离](https://leetcode-cn.com/problems/hamming-distance/)
2+
3+
两个整数之间的[汉明距离](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E8%B7%9D%E7%A6%BB)指的是这两个数字对应二进制位不同的位置的数目。
4+
5+
给出两个整数 `x``y`,计算它们之间的汉明距离。
6+
7+
**注意:**
8+
9+
0 ≤ `x`, `y` < 2
10+
31
11+
.
12+
13+
**示例:**
14+
15+
`
16+
**输入:** x = 1, y = 4
17+
18+
**输出:** 2
19+
20+
**解释:**
21+
1 (0 0 0 1)
22+
4 (0 1 0 0)
23+
↑ ↑
24+
25+
上面的箭头指出了对应二进制位不同的位置。
26+
`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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/简单/461/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input1 int
18+
input2 int
19+
want int
20+
}{
21+
{
22+
name: "test-1-4",
23+
input1: 1,
24+
input2: 4,
25+
want: 2,
26+
},
27+
}
28+
29+
testLog := leet.NewTestLog(len(tests))
30+
defer testLog.Render()
31+
32+
timeoutDuration := time.Second * 2
33+
34+
for idx, test := range tests {
35+
// 超时检测
36+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
37+
solution.Export(test.input1, test.input2)
38+
cancel()
39+
})
40+
41+
if timeout {
42+
testLog.Fail(idx+1, test.name, "timeout")
43+
continue
44+
}
45+
46+
got := solution.Export(test.input1, test.input2)
47+
if !reflect.DeepEqual(test.want, got) {
48+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
49+
continue
50+
}
51+
52+
testLog.Pass(idx+1, test.name)
53+
}
54+
}
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(x int, y int) int {
4+
return hammingDistance(x, y)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingDistance(x int, y int) 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(x int, y int) int {
4+
return hammingDistance(x, y)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingDistance(x int, y int) int {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## [汉明距离](https://leetcode-cn.com/problems/hamming-distance/)
2+
3+
两个整数之间的[汉明距离](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E8%B7%9D%E7%A6%BB)指的是这两个数字对应二进制位不同的位置的数目。
4+
5+
给出两个整数 `x``y`,计算它们之间的汉明距离。
6+
7+
**注意:**
8+
9+
0 ≤ `x`, `y` < 2
10+
31
11+
.
12+
13+
**示例:**
14+
15+
`
16+
**输入:** x = 1, y = 4
17+
18+
**输出:** 2
19+
20+
**解释:**
21+
1 (0 0 0 1)
22+
4 (0 1 0 0)
23+
↑ ↑
24+
25+
上面的箭头指出了对应二进制位不同的位置。
26+
`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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/简单/461/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input1 int
18+
input2 int
19+
want int
20+
}{
21+
{
22+
name: "test-1-4",
23+
input1: 1,
24+
input2: 4,
25+
want: 2,
26+
},
27+
}
28+
29+
testLog := leet.NewTestLog(len(tests))
30+
defer testLog.Render()
31+
32+
timeoutDuration := time.Second * 2
33+
34+
for idx, test := range tests {
35+
// 超时检测
36+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
37+
solution.Export(test.input1, test.input2)
38+
cancel()
39+
})
40+
41+
if timeout {
42+
testLog.Fail(idx+1, test.name, "timeout")
43+
continue
44+
}
45+
46+
got := solution.Export(test.input1, test.input2)
47+
if !reflect.DeepEqual(test.want, got) {
48+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
49+
continue
50+
}
51+
52+
testLog.Pass(idx+1, test.name)
53+
}
54+
}
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(x int, y int) int {
4+
return hammingDistance(x, y)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingDistance(x int, y int) 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(x int, y int) int {
4+
return hammingDistance(x, y)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func hammingDistance(x int, y int) int {
13+
14+
}

0 commit comments

Comments
 (0)