10000 Merge pull request #145 from kylesliu/master · ninefive/awesome-golang-leetcode@44d8039 · GitHub
[go: up one dir, main page]

Skip to content

Commit 44d8039

Browse files
author
Kyle Liu
authored
Merge pull request 6boris#145 from kylesliu/master
sync code
2 parents 49d5466 + 30724b7 commit 44d8039

File tree

99 files changed

+1461
-282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1461
-282
lines changed

src/0118.Pascals-Triangle/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: 5
12+
Output:
13+
[
14+
[1],
15+
[1,1],
16+
[1,2,1],
17+
[1,3,3,1],
18+
[1,4,6,4,1]
19+
]
20+
1321
```
1422

1523
## 题意

src/0118.Pascals-Triangle/Solution.go

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(numRows int) [][]int {
4+
pt := make([][]int, numRows)
5+
for i := range pt {
6+
pt[i] = make([]int, i+1)
7+
}
8+
for i, val := range pt {
9+
pt[i][0], pt[i][len(val)-1] = 1, 1
10+
}
11+
for i := 1; i < numRows-1; i++ {
12+
for j := 0; j < len(pt[i])-1; j++ {
13+
pt[i+1][j+1] = pt[i][j] + pt[i][j+1]
14+
}
15+
}
16+
return pt
517
}

src/0118.Pascals-Triangle/Solution_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ 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", 5, [][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}}},
17+
{"TestCase", 2, [][]int{{1}, {1, 1}}},
1918
}
2019

2120
// 开始测试

src/0122.Best-Time-To-Buy-And-Sell-Stock-II/Solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func maxProfit(prices []int) int {
2424
//dp[i][0] means the maximum profit after sunset of day i with no stock in hand
2525
//dp[i][1] means the maximum profit after sunset of day i with stock in hand
2626
//dp[i][0] = max(dp[i-1][1]+price[i],dp[i-1][0]) --> Sell on day i or do nothing
27-
//dp[i][1] = max(dp[i-1][0])-price[i],dp[i-1][1]) --> Buy on day i or do nothing
27+
//dp[i][1] = max(dp[i-1][0]-price[i],dp[i-1][1]) --> Buy on day i or do nothing
2828
//dp[0][0]=0,dp[0][1]=INT_MIN
2929

3030
func maxProfit2(prices []int) int {

src/0167.Two-Sum-II--Input-array-is-sorted/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: numbers = [2,7,11,15], target = 9
12+
Output: [1,2]
1313
```
1414

1515
## 题意
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(numbers []int, target int) []int {
4+
i, j := 0, len(numbers)-1
5+
for i < j {
6+
sum := numbers[i] + numbers[j]
7+
if sum < target {
8+
i++
9+
} else if sum > target {
10+
j--
11+
} else {
12+
return []int{i + 1, j + 1}
13+
}
14+
}
15+
return []int{0, 0}
516
}

src/0167.Two-Sum-II--Input-array-is-sorted/Solution_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
numbers []int
14+
target int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{2, 7, 11, 15}, 9, []int{1, 2}},
18+
{"TestCase", []int{2, 7, 11, 15}, 22, []int{2, 4}},
19+
{"TestCase", []int{2, 7, 11, 15}, 26, []int{3, 4}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.numbers, c.target)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.numbers, c.target)
2829
}
2930
})
3031
}

src/0268.Missing-Number/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: [3,0,1]
12+
Output: 2
1313
```
1414

1515
## 题意

src/0268.Missing-Number/Solution.go

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
ans := len(nums)
5+
for i, num := range nums {
6+
ans ^= i ^ num
7+
}
8+
return ans
59
}

src/0268.Missing-Number/Solution_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ 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{3, 0, 1}, 2},
17+
{"TestCase", []int{9, 6, 4, 2, 3, 5, 7, 0, 1}, 8},
1918
}
2019

2120
// 开始测试

src/0283.Move-Zeroes/Solution.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ func moveZeroes(nums []int) []int {
1414
}
1515
return nums
1616
}
17+
18+
func moveZeroes2(nums []int) []int {
19+
n := len(nums)
20+
if n > 1 {
21+
for z, nz := 0, 0; nz < n; nz++ {
22+
if nums[nz] != 0 {
23+
nums[z], nums[nz] = nums[nz], nums[z]
24+
z++
25+
}
26+
}
27+
}
28+
return nums
29+
}

src/0283.Move-Zeroes/Solution_test.go

Lines changed: 23 additions & 0 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

@@ -24,3 +25,25 @@ func TestMoveZeroes(t *testing.T) {
2425
})
2526
}
2627
}
28+
29+
func TestMoveZeroes2(t *testing.T) {
30+
cases := []struct {
31+
name string
32+
inputs []int
33+
expects []int
34+
}{
35+
{"TestCase", []int{0, 1, 0, 3, 12}, []int{1, 3, 12, 0, 0}},
36+
{"TestCase", []int{0}, []int{0}},
37+
{"TestCase", []int{1}, []int{1}},
38+
}
39+
40+
for i, testcase := range cases {
41+
t.Run(testcase.name+" "+strconv.Itoa(i), func(t *testing.T) {
42+
result := moveZeroes2(testcase.inputs)
43+
if !reflect.DeepEqual(result, testcase.expects) {
44+
t.Fatalf("expected: %v, but got: %v, with input: %v ", testcase.expects, result, testcase.inputs)
45+
}
46+
47+
})
48+
}
49+
}

src/0509.Fibonacci-Number/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: 2
12+
Output: 1
1313
```
1414

1515
## 题意

src/0509.Fibonacci-Number/Solution.go

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(N int) int {
4+
var ans int
5+
fibSeq := getFibSeq()
6+
for i := 0; i <= N; i++ {
7+
ans = fibSeq()
8+
}
9+
return ans
10+
}
11+
12+
func getFibSeq() func() int {
13+
a, b := -1, 1
14+
return func() int {
15+
c := a + b
16+
a, b = b, c
17+
return c
18+
}
519
}

src/0509.Fibonacci-Number/Solution_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ 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", 2, 1},
17+
{"TestCase", 3, 2},
18+
{"TestCase", 4, 3},
19+
{"TestCase", 0, 0},
1920
}
2021

2122
// 开始测试

src/0566.Reshape-the-Matrix/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [[1,2], [3,4]] r = 1, c = 4
12+
Output: [[1,2,3,4]]
1313
```
1414

1515
## 题意
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums [][]int, r int, c int) [][]int {
4+
m, n := len(nums), len(nums[0])
5+
if m*n != r*c {
6+
return nums
7+
}
8+
newNums := make([][]int, r)
9+
for i := range newNums {
10+
newNums[i] = make([]int, c)
11+
}
12+
k := 0
13+
for i := 0; i < m; i++ {
14+
for j := 0; j < n; j++ {
15+
r_, c_ := k/c, k%c
16+
newNums[r_][c_] = nums[i][j]
17+
k++
18+
}
19+
}
20+
return newNums
521
}

src/0566.Reshape-the-Matrix/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums [][]int
14+
r, c int
15+
expect [][]int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", [][]int{{1, 2}, {3, 4}}, 1, 4, [][]int{{1, 2, 3, 4}}},
18+
{"TestCase", [][]int{{1, 2}, {3, 4}}, 2, 4, [][]int{{1, 2}, {3, 4}}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.r, c.c)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.nums, c.r, c.c)
2828
}
2929
})
3030
}

src/0661.Image-Smoother/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input:
12+
[[1,1,1],
13+
[1,0,1],
14+
[1,1,1]]
15+
Output:
16+
[[0, 0, 0],
17+
[0, 0, 0],
18+
[0, 0, 0]]
1319
```
1420

1521
## 题意

0 commit comments

Comments
 (0)
0