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

Commit8e3c739

Browse files
author
Kyle Liu
authored
Merge pull request6boris#148 from sathishbabu96/master
New test cases and solution
2 parentscc98c59 +b858962 commit8e3c739

File tree

70 files changed

+1297
-190
lines changed

Some content is hidden

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

70 files changed

+1297
-190
lines changed

‎src/0066.Plus-One/Solution.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,20 @@ func plusOne(digits []int) []int {
1919
}
2020
returndigits
2121
}
22+
23+
funcSolution(digits []int) []int {
24+
n:=len(digits)
25+
ifn<1 {
26+
return []int{1}
27+
}
28+
digits[n-1]+=1
29+
carry:=0
30+
fori:=n-1;i>=0;i-- {
31+
n:=digits[i]+carry
32+
digits[i],carry=n%10,n/10
33+
}
34+
ifcarry>0 {
35+
digits=append([]int{carry},digits...)
36+
}
37+
returndigits
38+
}

‎src/0066.Plus-One/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

@@ -25,3 +26,25 @@ func TestSolution(t *testing.T) {
2526
})
2627
}
2728
}
29+
30+
funcTestSolution2(t*testing.T) {
31+
cases:= []struct {
32+
namestring
33+
input []int
34+
expect []int
35+
}{
36+
{"TestCase", []int{1,2,3}, []int{1,2,4}},
37+
{"TestCase", []int{9,9,9}, []int{1,0,0,0}},
38+
{"TestCase", []int{5,9,9}, []int{6,0,0}},
39+
{"TestCase", []int{}, []int{1}},
40+
}
41+
fori,c:=rangecases {
42+
t.Run(c.name+" "+strconv.Itoa(i+1),func(t*testing.T) {
43+
got:=Solution(c.input)
44+
if!reflect.DeepEqual(got,c.expect) {
45+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
46+
c.expect,got,c.input)
47+
}
48+
})
49+
}
50+
}

‎src/0119.Pascals-Triangle-II/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
66
##Description
77

8+
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
9+
10+
Note that the row index starts from 0.
11+
12+
In Pascal's triangle, each number is the sum of the two numbers directly above it.
13+
814
**Example 1:**
915

1016
```
11-
Input:a = "11", b = "1"
12-
Output:"100"
17+
Input:3
18+
Output:[1,3,3,1]
1319
```
1420

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

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(rowIndexint) []int {
4+
ifrowIndex==0 {
5+
return []int{1}
6+
}
7+
pt:=make([]int,0)
8+
fori:=0;i<rowIndex;i++ {
9+
tmp:=make([]int,0)
10+
tmp=append(tmp,1)
11+
forj:=1;j<i+1;j++ {
12+
tmp=append(tmp,pt[j]+pt[j-1])
13+
}
14+
tmp=append(tmp,1)
15+
pt=tmp
16+
}
17+
returnpt
518
}

‎src/0119.Pascals-Triangle-II/Solution_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
inputsint
14+
expect[]int
1515
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
16+
{"TestCase",0, []int{1}},
17+
{"TestCase",3, []int{1,3,3,1}},
18+
{"TestCase",5, []int{1,5,10,10,5,1}},
1919
}
2020

2121
//开始测试

‎src/0189.Rotate-Array/README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,33 @@
55
66
##Description
77

8+
Given an array, rotate the array to the right by k steps, where k is non-negative.
9+
10+
Follow up:
11+
12+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
13+
Could you do it in-place with O(1) extra space?
14+
15+
816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: nums = [1,2,3,4,5,6,7], k = 3
20+
Output: [5,6,7,1,2,3,4]
21+
Explanation:
22+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
23+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
24+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: nums = [-1,-100,3,99], k = 2
31+
Output: [3,99,-1,-100]
32+
Explanation:
33+
rotate 1 steps to the right: [99,-1,-100,3]
34+
rotate 2 steps to the right: [3,99,-1,-100]
1335
```
1436

1537
##题意

‎src/0189.Rotate-Array/Solution.go

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

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(nums []int,kint) {
4+
n:=len(nums)
5+
ifk==0||k%n==0 {
6+
return
7+
}
8+
k=k%n
9+
reverse(nums,0,n-1)
10+
reverse(nums,0,k-1)
11+
reverse(nums,k,n-1)
12+
}
13+
14+
funcreverse(arr []int,x,yint) {
15+
fori,j:=x,y;i<=j;i,j=i+1,j-1 {
16+
arr[i],arr[j]=arr[j],arr[i]
17+
}
518
}

‎src/0189.Rotate-Array/Solution_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
nums []int
14+
kint
15+
expect []int
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase", []int{1,2,3,4,5,6,7},3, []int{5,6,7,1,2,3,4}},
18+
{"TestCase", []int{1,2,3,4,5,6,7},7, []int{1,2,3,4,5,6,7}},
19+
{"TestCase", []int{1,2,3,4,5,6,7},21, []int{1,2,3,4,5,6,7}},
20+
{"TestCase", []int{1,2,3,4,5,6,7},6, []int{2,3,4,5,6,7,1}},
21+
{"TestCase", []int{1,2,3,4,5,6,7},20, []int{2,3,4,5,6,7,1}},
1922
}
2023

2124
//开始测试
2225
fori,c:=rangecases {
2326
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
25-
if!reflect.DeepEqual(got,c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
27+
tmp:=c.nums
28+
Solution(c.nums,c.k)
29+
if!reflect.DeepEqual(c.nums,c.expect) {
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
31+
c.expect,c.nums,tmp,c.k)
2832
}
2933
})
3034
}

‎src/0198.House-Robber/Solution.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ func rob3(nums []int) int {
3636
returnmax(robEven,robOdd)
3737
}
3838

39+
funcrob4(nums []int)int {
40+
n:=len(nums)
41+
ifn==0 {
42+
return0
43+
}
44+
ifn==1 {
45+
returnnums[0]
46+
}
47+
dp:=make([]int,n)
48+
dp[0],dp[1]=nums[0],max(nums[0],nums[1])
49+
fori:=2;i<n;i++ {
50+
dp[i]=max(nums[i]+dp[i-2],dp[i-1])
51+
}
52+
returndp[n-1]
53+
}
54+
3955
funcmax(x,yint)int {
4056
ifx>y {
4157
returnx

‎src/0198.House-Robber/Solution_test.go

Lines changed: 27 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

@@ -75,6 +76,32 @@ func TestSolution3(t *testing.T) {
7576
}
7677
}
7778

79+
funcTestSolution4(t*testing.T) {
80+
//测试用例
81+
cases:= []struct {
82+
namestring
83+
inputs []int
84+
expectint
85+
}{
86+
{"TestCacse", []int{},0},
87+
{"TestCacse", []int{3},3},
88+
{"TestCacse", []int{1,2,3,1},4},
89+
{"TestCacse", []int{2,7,9,3,1},12},
90+
{"TestCacse", []int{5,2,6,7,3,1},14},
91+
}
92+
93+
//开始测试
94+
fori,c:=rangecases {
95+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
96+
got:=rob4(c.inputs)
97+
if!reflect.DeepEqual(got,c.expect) {
98+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
99+
c.expect,got,c.inputs)
100+
}
101+
})
102+
}
103+
}
104+
78105
//压力测试
79106
funcBenchmarkSolution(b*testing.B) {
80107

‎src/0219.Contains-Duplicate-II/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,27 @@
55
66
##Description
77

8+
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
9+
810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,2,3,1], k = 3
14+
Output: true
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: nums = [1,0,1,1], k = 1
21+
Output: true
22+
```
23+
24+
**Example 3:**
25+
26+
```
27+
Input: nums = [1,2,3,1,2,3], k = 2
28+
Output: false
1329
```
1430

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

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(nums []int,kint)bool {
4+
hash:=make(map[int]int)
5+
fori,num:=rangenums {
6+
ifv,ok:=hash[num];ok {
7+
ifi-v<=k {
8+
returntrue
9+
}
10+
}
11+
hash[num]=i
12+
}
13+
returnfalse
514
}

‎src/0219.Contains-Duplicate-II/Solution_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
13+
nums []int
14+
kint
1415
expectbool
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase",[]int{1,2,3,1},3,true},
18+
{"TestCase",[]int{1,0,1,1},1,true},
19+
{"TestCase",[]int{1,2,3,1,2,3},2,false},
1920
}
2021

2122
//开始测试
2223
fori,c:=rangecases {
2324
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
25+
got:=Solution(c.nums,c.k)
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.nums,c.k)
2829
}
2930
})
3031
}

‎src/0414.Third-Maximum-Number/README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,37 @@
55
66
##Description
77

8+
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
9+
810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: [3, 2, 1]
14+
15+
Output: 1
16+
17+
Explanation: The third maximum is 1.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [1, 2]
24+
25+
Output: 2
26+
27+
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: [2, 2, 3, 1]
34+
35+
Output: 1
36+
37+
Explanation: Note that the third maximum here means the third maximum distinct number.
38+
Both numbers with value 2 are both considered as second maximum.
1339
```
1440

1541
##题意

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp