Skip to content

Commit d74da70

Browse files
committed
add some solution for offer
1 parent ed6ba91 commit d74da70

File tree

7 files changed

+204
-44
lines changed

7 files changed

+204
-44
lines changed

nowcoder/jz-offer/jz006/README.md renamed to lcof/of011/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ6.旋转数组的最小数字][title]
1+
# [OF0.Template Title][title]
22

33
> [!WARNING|style:flat]
44
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
@@ -28,5 +28,5 @@ Output: 1
2828

2929
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
3030

31-
[title]: https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba/
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
3232
[me]: https://github.com/kylesliu/awesome-golang-algorithm

nowcoder/jz-offer/jz006/Solution.go renamed to lcof/of011/Solution.go

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

3-
// 二分查找
4-
func minNumberInRotateArray(arr []int) int {
3+
func minArray(arr []int) int {
54
if len(arr) == 0 {
65
return 0
76
}

lcof/of011/Solution_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
minArray,
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: []int{3, 4, 5, 1, 2},
29+
expect: 1,
30+
},
31+
{
32+
name: "TestCase 2",
33+
inputs: []int{2, 2, 2, 0, 1},
34+
expect: 0,
35+
},
36+
{
37+
name: "TestCase 3",
38+
inputs: []int{},
39+
expect: 0,
40+
},
41+
{
42+
name: "TestCase 4",
43+
inputs: []int{6501, 6828, 6963, 7036, 7422, 7674, 8146, 8468, 8704, 8717, 9170, 9359, 9719, 9895, 9896, 9913, 9962, 154, 293, 334, 492, 1323, 1479, 1539, 1727, 1870, 1943, 2383, 2392, 2996, 3282, 3812, 3903, 4465, 4605, 4665, 4772, 4828, 5142, 5437, 5448, 5668, 5706, 5725, 6300, 6335},
44+
expect: 154,
45+
},
46+
}
47+
48+
// TestSolution Example for solution test cases
49+
func TestSolution(t *testing.T) {
50+
ast := assert.New(t)
51+
52+
for _, f := range SolutionFuncList {
53+
for _, c := range cases {
54+
t.Run(c.name, func(t *testing.T) {
55+
actual := f(c.inputs)
56+
ast.Equal(c.expect, actual,
57+
"func: %v case: %v ",
58+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
59+
})
60+
}
61+
}
62+
}

lcof/of012/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/of012/Solution.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package Solution
2+
3+
func exist(board [][]byte, word string) bool {
4+
m, n := len(board), len(board[0])
5+
for i := 0; i < m; i++ {
6+
for j := 0; j < n; j++ {
7+
//如果在数组中找得到第一个数,就执行下一步,否则返回false
8+
if search(board, i, j, 0, word) {
9+
return true
10+
}
11+
}
12+
}
13+
return false
14+
}
15+
func search(board [][]byte, i, j, k int, word string) bool {
16+
//如果找到最后一个数,则返回true,搜索成功
17+
if k == len(word) {
18+
return true
19+
}
20+
//i,j的约束条件
21+
if i < 0 || j < 0 || i == len(board) || j == len(board[0]) {
22+
return false
23+
}
24+
25+
//进入DFS深度优先搜索
26+
//先把正在遍历的该值重新赋值,如果在该值的周围都搜索不到目标字符,则再把该值还原
27+
//如果在数组中找到第一个字符,则进入下一个字符的查找
28+
if board[i][j] == word[k] {
29+
temp := board[i][j]
30+
board[i][j] = ' '
31+
//下面这个if语句,如果成功进入,说明找到该字符,然后进行下一个字符的搜索,直到所有的搜索都成功,
32+
//即k == len(word) - 1 的大小时,会返回true,进入该条件语句,然后返回函数true值。
33+
if search(board, i, j+1, k+1, word) ||
34+
search(board, i, j-1, k+1, word) ||
35+
search(board, i+1, j, k+1, word) ||
36+
search(board, i-1, j, k+1, word) {
37+
return true
38+
} else {
39+
//没有找到目标字符,还原之前重新赋值的board[i][j]
40+
board[i][j] = temp
41+
}
42+
}
43+
44+
return false
45+
}

lcof/of012/Solution_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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([][]byte, string) bool
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
exist,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
board [][]byte
21+
word string
22+
expect bool
23+
}
24+
25+
// test case
26+
var cases = []Case{
27+
{
28+
name: "TestCase 1",
29+
board: [][]byte{
30+
[]byte{'A', 'B', 'C', 'E'},
31+
[]byte{'S', 'F', 'C', 'S'},
32+
[]byte{'A', 'D', 'E', 'E'},
33+
},
34+
word: "ABCCED",
35+
expect: true,
36+
},
37+
{
38+
name: "TestCase 2",
39+
board: [][]byte{
40+
[]byte{'a', 'b'},
41+
[]byte{'c', 'd'},
42+
},
43+
word: "abcd",
44+
expect: false,
45+
},
46+
}
47+
48+
// TestSolution Example for solution test cases
49+
func TestSolution(t *testing.T) {
50+
ast := assert.New(t)
51+
52+
for _, f := range SolutionFuncList {
53+
for _, c := range cases {
54+
t.Run(c.name, func(t *testing.T) {
55+
actual := f(c.board, c.word)
56+
ast.Equal(c.expect, actual,
57+
"func: %v case: %v ",
58+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
59+
})
60+
}
61+
}
62+
}

nowcoder/jz-offer/jz006/Solution_test.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)