Movatterモバイル変換


[0]ホーム

URL:


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

Commitf260fcb

Browse files
committed
add some solution
1 parent9c3b944 commitf260fcb

File tree

24 files changed

+351
-84
lines changed

24 files changed

+351
-84
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[1. Template Title][title]
2+
3+
>[!WARNING|style:flat]
4+
>This problem is temporarily not PR, please submit[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-leetcode)
5+
6+
##Description
7+
8+
**Example 1:**
9+
10+
```
11+
Input: a = "11", b = "1"
12+
Output: "100"
13+
```
14+
15+
##题意
16+
>...
17+
18+
##题解
19+
20+
###思路1
21+
>...
22+
23+
```go
24+
```
25+
26+
27+
##结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
30+
31+
[title]:https://leetcode.com/problems/two-sum/
32+
[me]:https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Solution
2+
3+
funcsingleNumber(nums []int)int {
4+
m:=make(map[int]int,len(nums))
5+
for_,v:=rangenums {
6+
m[v]++
7+
}
8+
forv:=rangem {
9+
ifm[v]==1 {
10+
returnv
11+
}
12+
}
13+
return0
14+
}
15+
16+
//只循环一次
17+
// 2∗(a+b+c)−(a+a+b+b+c) = c
18+
funcsingleNumber2(nums []int)int {
19+
m:=make(map[int]int,len(nums))
20+
sum1,sum2:=0,0
21+
for_,v:=rangenums {
22+
if_,ok:=m[v];!ok {
23+
m[v]++
24+
sum1+=v
25+
}
26+
sum2+=v
27+
}
28+
return2*sum1-sum2
29+
}
30+
31+
//用异或
32+
// 0^0 = 0,
33+
// 1^0 = 1,
34+
// 0^1 = 1,
35+
// 1^1 = 0
36+
37+
funcsingleNumber3(nums []int)int {
38+
ans:=0
39+
for_,v:=rangenums {
40+
ans^=v
41+
}
42+
returnans
43+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
funcTestSolution(t*testing.T) {
10+
//测试用例
11+
cases:= []struct {
12+
namestring
13+
inputs []int
14+
expectint
15+
}{
16+
{"TestCase", []int{2,2,1},1},
17+
{"TestCase", []int{4,1,2,1,2},4},
18+
}
19+
20+
//开始测试
21+
fori,c:=rangecases {
22+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
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+
funcTestSolution2(t*testing.T) {
33+
//测试用例
34+
cases:= []struct {
35+
namestring
36+
inputs []int
37+
expectint
38+
}{
39+
{"TestCase", []int{2,2,1},1},
40+
{"TestCase", []int{4,1,2,1,2},4},
41+
}
42+
43+
//开始测试
44+
fori,c:=rangecases {
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+
funcTestSolution3(t*testing.T) {
56+
//测试用例
57+
cases:= []struct {
58+
namestring
59+
inputs []int
60+
expectint
61+
}{
62+
{"TestCase", []int{2,2,1},1},
63+
{"TestCase", []int{4,1,2,1,2},4},
64+
}
65+
66+
//开始测试
67+
fori,c:=rangecases {
68+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
69+
got:=singleNumber3(c.inputs)
70+
if!reflect.DeepEqual(got,c.expect) {
71+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
72+
c.expect,got,c.inputs)
73+
}
74+
})
75+
}
76+
}
77+
78+
//压力测试
79+
funcBenchmarkSolution(b*testing.B) {
80+
81+
}
82+
83+
//使用案列
84+
funcExampleSolution() {
85+
86+
}

‎cmd/leetcode/makedir_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
funcTestMakeDir(t*testing.T) {
9-
problems:=GetSotedproblemsInstance()
9+
problems:=GetSortedProblemsInstance()
1010
fmt.Println(len(problems))
1111

1212
for_,v:=rangeproblems {

‎cmd/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package main
33
import (
44
"fmt"
55
"github.com/kylesliu/awesome-golang-leetcode/cmd/leetcode"
6-
"github.com/kylesliu/awesome-golang-leetcode/cmd/sitemap"
76
)
87

98
funcmain() {
109
problems:=leetcode.GetSortedProblemsInstance()
11-
//fmt.Println(v)
12-
//}for _,v := range problems{
13-
10+
//fmt.Println(v)
11+
for_,v:=rangeproblems {
12+
fmt.Println(v)
13+
}
1414
//problems := leetcode.GetProblemsJson()
1515
//fmt.Println(problems)
1616
//for _, v := range problems {
@@ -32,7 +32,7 @@ func main() {
3232
//leetcode.MakeGitbookSummary(problems)
3333

3434
//sitemap
35-
s:=sitemap.New(problems)
36-
fmt.Println(s)
35+
//s := sitemap.New(problems)
36+
//fmt.Println(s)
3737

3838
}

‎lib/ListNode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type ListNode struct {
1111
}
1212

1313
//比较结果
14-
funcisEqual(l1*ListNode,l2*ListNode)bool {
14+
funcIsEqual(l1*ListNode,l2*ListNode)bool {
1515

1616
forl1!=nil&&l2!=nil {
1717
ifl1.Val!=l2.Val {

‎lib/kmp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Solution
2+
3+
funcmain() {
4+
5+
}

‎lib/kmp_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package Solution

‎src/0002.Add-Two-Numbers/Solution_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package Solution
22

33
import (
4+
"strconv"
45
"testing"
56
)
67

@@ -12,14 +13,15 @@ func TestSolution(t *testing.T) {
1213
input2*ListNode
1314
expect*ListNode
1415
}{
15-
{"1 test 1",&ListNode{Val:2,Next:&ListNode{Val:4,Next:&ListNode{Val:3,Next:nil}}},
16+
{"Test Case ",
17+
&ListNode{Val:2,Next:&ListNode{Val:4,Next:&ListNode{Val:3,Next:nil}}},
1618
&ListNode{Val:5,Next:&ListNode{Val:6,Next:&ListNode{Val:4,Next:nil}}},
1719
&ListNode{Val:7,Next:&ListNode{Val:0,Next:&ListNode{Val:8,Next:nil}}}},
1820
}
1921

2022
//开始测试
21-
for_,c:=rangecases {
22-
t.Run(c.name,func(t*testing.T) {
23+
fori,c:=rangecases {
24+
t.Run(c.name+strconv.Itoa(i),func(t*testing.T) {
2325
got:=addTwoNumbers(c.input1,c.input2)
2426
if!isEqual(got,c.expect) {
2527
t.Fatalf("expected: %v, but got: %v, with inputs: %v",

‎src/0015.3Sum/Solution_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package Solution
22

33
import (
44
"reflect"
5+
"strconv"
56
"testing"
67
)
78

@@ -12,18 +13,28 @@ func TestSolution(t *testing.T) {
1213
inputs []int
1314
expect [][]int
1415
}{
15-
{"1 test 1", []int{-1,0,1,2,-1,-4}, [][]int{{-1,-1,2}, {-1,0,1}}},
16-
{"1 test 1", []int{-2,0,0,2,2}, [][]int{{-2,0,2}}},
16+
{"TestCase", []int{-1,0,1,2,-1,-4}, [][]int{{-1,-1,2}, {-1,0,1}}},
17+
{"TestCase", []int{-2,0,0,2,2}, [][]int{{-2,0,2}}},
1718
}
1819

1920
//开始测试
20-
for_,c:=rangecases {
21-
t.Run(c.name,func(t*testing.T) {
22-
ret:=threeSum(c.inputs)
23-
if!reflect.DeepEqual(ret,c.expect) {
21+
fori,c:=rangecases {
22+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
23+
got:=threeSum(c.inputs)
24+
if!reflect.DeepEqual(got,c.expect) {
2425
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
25-
c.expect,ret,c.inputs)
26+
c.expect,got,c.inputs)
2627
}
2728
})
2829
}
2930
}
31+
32+
//压力测试
33+
funcBenchmarkSolution(b*testing.B) {
34+
35+
}
36+
37+
//使用案列
38+
funcExampleSolution() {
39+
40+
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcplusOne(digits []int) []int {
4+
n:=len(digits)
5+
fori:=n-1;i>=0;i-- {
6+
ifdigits[i]==9 {
7+
digits[i]=0
8+
}else {
9+
digits[i]+=1
10+
returndigits
11+
}
12+
}
13+
ans:=make([]int,n+1)
14+
ans[0]=1
15+
returnans
516
}

‎src/0060.Permutation-Sequence/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@ package Solution
22

33
import (
44
"reflect"
5+
"strconv"
56
"testing"
67
)
78

89
funcTestSolution(t*testing.T) {
910
//测试用例
1011
cases:= []struct {
1112
namestring
12-
inputsbool
13-
expectbool
13+
inputs[]int
14+
expect[]int
1415
}{
15-
{"TestCacse 1",true,true},
16-
{"TestCacse 1",true,true},
17-
{"TestCacse 1",false,false},
16+
{"TestCase", []int{1,2,3}, []int{1,2,4}},
1817
}
1918

2019
//开始测试
21-
for_,c:=rangecases {
22-
t.Run(c.name,func(t*testing.T) {
23-
ret:=Solution(c.inputs)
24-
if!reflect.DeepEqual(ret,c.expect) {
20+
fori,c:=rangecases {
21+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
22+
got:=plusOne(c.inputs)
23+
if!reflect.DeepEqual(got,c.expect) {
2524
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect,ret,c.inputs)
25+
c.expect,got,c.inputs)
2726
}
2827
})
2928
}

‎src/0100.Same-Tree/Solution_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ func TestSolution(t *testing.T) {
1515
input2*TreeNode
1616
expectbool
1717
}{
18-
{"TestCacse 1",
18+
{"TestCase 1",
1919
&TreeNode{Val:1,
2020
Left:&TreeNode{Val:2,Left:nil,Right:nil},
2121
Right:&TreeNode{Val:3,Left:nil,Right:nil}},
2222
&TreeNode{Val:1,
2323
Left:&TreeNode{Val:2,Left:nil,Right:nil},
2424
Right:&TreeNode{Val:3,Left:nil,Right:nil}},
2525
true},
26-
{"TestCacse 2",
26+
{"TestCase 2",
2727
&TreeNode{Val:1,
2828
Left:&TreeNode{Val:2,Left:nil,Right:nil},
2929
Right:nil},
3030
&TreeNode{Val:1,
3131
Left:nil,
3232
Right:&TreeNode{Val:2,Left:nil,Right:nil}},
3333
true},
34-
{"TestCacse 3",
34+
{"TestCase 3",
3535
&TreeNode{Val:1,
3636
Left:&TreeNode{Val:2,Left:nil,Right:nil},
3737
Right:nil},

‎src/0104.Maximum-Depth-of-Binary-Tree/Solution_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ func TestSolution(t *testing.T) {
1212
inputsbool
1313
expectbool
1414
}{
15-
{"1 test 1",true,true},
16-
{"2 test 2",true,true},
17-
{"3 test 3",false,false},
15+
{"TestCase",true,true},
1816
}
1917

2018
//开始测试

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp