Skip to content

Commit 27068e4

Browse files
committed
add some solution
1 parent 7b94541 commit 27068e4

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

lcof/of004/Solution.go

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

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])
69

710
// 从右上角的元素找起来
811
for r, c := 0, cLen-1; r < rLen && c >= 0; {
9-
if board[r][c] == target {
12+
if matrix[r][c] == target {
1013
return true
1114
}
12-
if board[r][c] > target {
15+
if matrix[r][c] > target {
1316
c--
1417
} else {
1518
r++

lcof/of064/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

lcof/of064/Solution.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

lcof/of064/Solution_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

0 commit comments

Comments
 (0)