File tree 4 files changed +100
-5
lines changed 4 files changed +100
-5
lines changed Original file line number Diff line number Diff line change 1
1
package Solution
2
2
3
- func findNumberIn2DArray (board [][]int , target int ) bool {
4
- rLen := len (board )
5
- cLen := len (board [0 ])
3
+ func findNumberIn2DArray (matrix [][]int , target int ) bool {
4
+ if len (matrix ) == 0 {
5
+ return false
6
+ }
7
+ rLen := len (matrix )
8
+ cLen := len (matrix [0 ])
6
9
7
10
// 从右上角的元素找起来
8
11
for r , c := 0 , cLen - 1 ; r < rLen && c >= 0 ; {
9
- if board [r ][c ] == target {
12
+ if matrix [r ][c ] == target {
10
13
return true
11
14
}
12
- if board [r ][c ] > target {
15
+ if matrix [r ][c ] > target {
13
16
c --
14
17
} else {
15
18
r ++
Original file line number Diff line number Diff line change
1
+ # [ OF0.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-algorithm )
5
+
6
+
7
+ ## 题目描述
8
+ ....
9
+
10
+ ** 范例 :**
11
+
12
+ ```
13
+ Input: n=1
14
+ Output: 1
15
+ ```
16
+
17
+ ## 题意
18
+ > ...
19
+
20
+ ## 题解
21
+
22
+ ### 思路1
23
+ > ...
24
+ ``` go
25
+ ```
26
+
27
+ ## 结语
28
+
29
+ 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[ awesome-golang-algorithm] [ me ]
30
+
31
+ [ title ] : https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32
+ [ me ] : https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
1
+ package Solution
2
+
3
+ func sumNums (n int ) int {
4
+ if n == 0 {
5
+ return 0
6
+ }
7
+ return n + sumNums (n - 1 )
8
+ }
Original file line number Diff line number Diff line change
1
+ package Solution
2
+
3
+ import (
4
+ "github.com/stretchr/testify/assert"
5
+ "reflect"
6
+ "runtime"
7
+ "testing"
8
+ )
9
+
10
+ // solution func Info
11
+ type SolutionFuncType func (int ) int
12
+
13
+ var SolutionFuncList = []SolutionFuncType {
14
+ sumNums ,
15
+ }
16
+
17
+ // test info struct
18
+ type Case struct {
19
+ name string
20
+ inputs int
21
+ expect int
22
+ }
23
+
24
+ // test case
25
+ var cases = []Case {
26
+ {
27
+ name : "TestCase 1" ,
28
+ inputs : 3 ,
29
+ expect : 6 ,
30
+ },
31
+ {
32
+ name : "TestCase 2" ,
33
+ inputs : 9 ,
34
+ expect : 45 ,
35
+ },
36
+ }
37
+
38
+ // TestSolution Example for solution test cases
39
+ func TestSolution (t * testing.T ) {
40
+ ast := assert .New (t )
41
+
42
+ for _ , f := range SolutionFuncList {
43
+ for _ , c := range cases {
44
+ t .Run (c .name , func (t * testing.T ) {
45
+ actual := f (c .inputs )
46
+ ast .Equal (c .expect , actual ,
47
+ "func: %v case: %v " ,
48
+ runtime .FuncForPC (reflect .ValueOf (f ).Pointer ()).Name (), c .name )
49
+ })
50
+ }
51
+ }
52
+ }
You can’t perform that action at this time.
0 commit comments