8000 add 136 solution · deardeng/awesome-golang-leetcode@a33b5ff · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit a33b5ff

Browse files
committed
add 136 solution
1 parent ab3bad6 commit a33b5ff

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

src/0136.Single-Number/Solution.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func singleNumber(nums []int) int {
4+
m := make(map[int]int, len(nums))
5+
for _, v := range nums {
6+
m[v]++
7+
}
8+
for v := range m {
9+
if m[v] == 1 {
10+
return v
11+
}
12+
}
13+
return 0
14+
}
15+
16+
// 只循环一次
17+
// 2∗(a+b+c)−(a+a+b+b+c) = c
18+
func singleNumber2(nums []int) int {
19+
m := make(map[int]int, len(nums))
20+
sum1, sum2 := 0, 0
21+
for _, v := range nums {
22+
if _, ok := m[v]; !ok {
23+
m[v]++
24+
sum1 += v
25+
}
26+
sum2 += v
27+
}
28+
return 2*sum1 - sum2
29+
}
30+
31+
// 用异或
32+
// 0^0 = 0,
33+
// 1^0 = 1,
34+
// 0^1 = 1,
35+
// 1^1 = 0
36+
37+
func singleNumber3(nums []int) int {
38+
ans := 0
39+
for _, v := range nums {
40+
ans ^= v
41+
}
42+
return ans
543
}

src/0136.Single-Number/Solution_test.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,63 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", []int{2, 2, 1}, 1},
17+
{"TestCase", []int{4, 1, 2, 1, 2}, 4},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := singleNumber(c.inputs)
24+
if !reflect.DeepEqual(got, c.expect) {
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26+
c.expect, got, c.inputs)
27+
}
28+
})
29+
}
30+
}
31+
32+
func TestSolution2(t *testing.T) {
33+
// 测试用例
34+
cases := []struct {
35+
name string
36+
inputs []int
37+
expect int
38+
}{
39+
{"TestCase", []int{2, 2, 1}, 1},
40+
{"TestCase", []int{4, 1, 2, 1, 2}, 4},
41+
}
42+
43+
// 开始测试
44+
for i, c := range cases {
45+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
46+
got := singleNumber2(c.inputs)
47+
if !reflect.DeepEqual(got, c.expect) {
48+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
49+
c.expect, got, c.inputs)
50+
}
51+
})
52+
}
53+
}
54+
55+
func TestSolution3(t *testing.T) {
56+
// 测试用例
57+
cases := []struct {
58+
name string
59+
inputs []int
60+
expect int
61+
}{
62+
{"TestCase", []int{2, 2, 1}, 1},
63+
{"TestCase", []int{4, 1, 2, 1, 2}, 4},
64+
}
65+
66+
// 开始测试
67+
for i, c := range cases {
68+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
69+
got := singleNumber3(c.inputs)
2570
if !reflect.DeepEqual(got, c.expect) {
2671
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2772
c.expect, got, c.inputs)

0 commit comments

Comments
 (0)
0