Skip to content

Commit ed6ba91

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

Some content is hidden

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

50 files changed

+1099
-525
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ LeetCode of algorithms with golang solution(updating:smiley:).
4646
- [LeetCode-in-Go](https://github.com/aQuaYi/LeetCode-in-Go) 某位算法大佬的Golang题解
4747
- [ACWING](https://www.acwing.com/) 一些算法竞赛大佬创建的平台,挺适合入门的。
4848
- [剑指Offer](https://www.nowcoder.com/ta/coding-interviews) 一些算法竞赛大佬创建的平台,挺适合入门的。
49+
- [剑指Offer](https://leetcode-cn.com/problemset/lcof/) 一些算法竞赛大佬创建的平台,挺适合入门的。
4950

5051
## Contributors
5152

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/kylesliu/awesome-golang-leetcode
1+
module github.com/kylesliu/awesome-golang-algorithm
22

33
go 1.15
44

lcof/of000/Solution_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var cases = []Case{
3030
},
3131
{
3232
name: "TestCase 2",
33-
inputs: true,
33+
inputs: false,
3434
expect: false,
3535
},
3636
}

nowcoder/jz-offer/jz003/README.md renamed to lcof/of003/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ0.Template Title][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)

lcof/of003/Solution.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Solution
2+
3+
import "sort"
4+
5+
// 排序 + 遍历
6+
func findRepeatNumber(nums []int) int {
7+
sort.Ints(nums)
8+
for idx, _ := range nums {
9+
if idx > 0 {
10+
if nums[idx] == nums[idx-1] {
11+
return nums[idx]
12+
}
13+
}
14+
}
15+
return 0
16+
}
17+
18+
// map哈希表
19+
func findRepeatNumber2(nums []int) int {
20+
mapTmp := make(map[int]bool)
21+
for _, val := range nums {
22+
if mapTmp[val] {
23+
return val
24+
} else {
25+
mapTmp[val] = true
26+
}
27+
}
28+
return 0
29+
}
30+
31+
// 原地置换
32+
func findRepeatNumber3(nums []int) int {
33+
for idx, val := range nums {
34+
if idx == val {
35+
continue
36+
}
37+
if nums[val] == val {
38+
return val
39+
}
40+
nums[val], nums[idx] = nums[idx], nums[val]
41+
}
42+
return 0
43+
}

lcof/of003/Solution_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package Solution
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"reflect"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
type SolutionFuncType func([]int) int
11+
12+
// Solution func
13+
var SolutionFuncList = []SolutionFuncType{
14+
findRepeatNumber,
15+
findRepeatNumber2,
16+
findRepeatNumber3,
17+
}
18+
19+
// test info struct
20+
type Case struct {
21+
name string
22+
inputs []int
23+
expect int
24+
}
25+
26+
// test case
27+
var cases = []Case{
28+
{
29+
name: "TestCase 1",
30+
inputs: []int{},
31+
expect: 0,
32+
},
33+
{
34+
name: "TestCase 2",
35+
inputs: []int{2, 1, 1, 0, 2, 5, 3},
36+
expect: 1,
37+
},
38+
{
39+
name: "TestCase 2",
40+
inputs: []int{1, 1, 0, 2, 5, 3},
41+
expect: 1,
42+
},
43+
}
44+
45+
// TestSolution Example for solution test cases
46+
func TestSolution(t *testing.T) {
47+
ast := assert.New(t)
48+
49+
for _, f := range SolutionFuncList {
50+
for _, c := range cases {
51+
t.Run(c.name, func(t *testing.T) {
52+
actual := f(c.inputs)
53+
ast.Equal(c.expect, actual,
54+
"func: %v case: %v ",
55+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
56+
})
57+
}
58+
}
59+
60+
}

nowcoder/jz-offer/jz008/README.md renamed to lcof/of004/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ0.跳台阶][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/8c82a5b80378478f9484d87d1c5f12a4/
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
3232
[me]: https://github.com/kylesliu/awesome-golang-algorithm

nowcoder/jz-offer/jz001/Solution.go renamed to lcof/of004/Solution.go

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

3-
func Find(board [][]int, target int) bool {
4-
rlen := len(board)
5-
clen := len(board[0])
3+
func findNumberIn2DArray(board [][]int, target int) bool {
4+
rLen := len(board)
5+
cLen := len(board[0])
66

77
// 从右上角的元素找起来
8-
for r, c := 0, clen-1; r < rlen && c >= 0; {
8+
for r, c := 0, cLen-1; r < rLen && c >= 0; {
99
if board[r][c] == target {
1010
return true
1111
}

lcof/of004/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) bool
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
findNumberIn2DArray,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
board [][]int
21+
target int
22+
expect bool
23+
}
24+
25+
// test case
26+
var cases = []Case{
27+
{
28+
name: "TestCase 1",
29+
board: [][]int{
30+
[]int{1, 2, 8, 9},
31+
},
32+
target: 7,
33+
expect: false,
34+
},
35+
{
36+
name: "TestCase 2",
37+
board: [][]int{
38+
[]int{1, 2, 8, 9},
39+
[]int{2, 4, 9, 12},
40+
[]int{4, 7, 10, 13},
41+
[]int{6, 8, 11, 15},
42+
},
43+
target: 7,
44+
expect: true,
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.target)
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/jz002/README.md renamed to lcof/of005/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ0.替换空格][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/4060ac7e3e404ad1a894ef3e17650423/
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
3232
[me]: https://github.com/kylesliu/awesome-golang-algorithm

lcof/of005/Solution.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Solution
2+
3+
func replaceSpace(s string) string {
4+
result := make([]rune, len(s)*3)
5+
6+
i := 0
7+
for _, v := range s {
8+
if v != ' ' {
9+
result[i] = v
10+
i++
11+
} else {
12+
result[i] = '%'
13+
result[i+1] = '2'
14+
result[i+2] = '0'
15+
i += 3
16+
}
17+
}
18+
return string(result)[:i]
19+
}

lcof/of005/Solution_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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(string) string
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
replaceSpace,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
inputs string
21+
expect string
22+
}
23+
24+
// test case
25+
var cases = []Case{
26+
{
27+
name: "TestCase 1",
28+
inputs: "We are happy.",
29+
expect: "We%20are%20happy.",
30+
},
31+
}
32+
33+
// TestSolution Example for solution test cases
34+
func TestSolution(t *testing.T) {
35+
ast := assert.New(t)
36+
37+
for _, f := range SolutionFuncList {
38+
for _, c := range cases {
39+
t.Run(c.name, func(t *testing.T) {
40+
actual := f(c.inputs)
41+
ast.Equal(c.expect, actual,
42+
"func: %v case: %v ",
43+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
44+
})
45+
}
46+
}
47+
}

nowcoder/jz-offer/jz004/README.md renamed to lcof/of006/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ4.重建二叉树][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/8a19cbe657394eeaac2f6ea9b0f6fcf6
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
3232
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
package Solution
22

3-
type NodeList struct {
3+
type ListNode struct {
44
Val int
5-
Next *NodeList
5+
Next *ListNode
66
}
77

88
// 递归写法
9-
func printListFromTailToHead(head *NodeList) []int {
9+
func printListFromTailToHead(head *ListNode) []int {
1010
ans := make([]int, 0)
1111
if head == nil {
1212
return ans
1313
}
1414
ans = printListFromTailToHead(head.Next)
1515
ans = append(ans, head.Val)
16-
1716
return ans
1817
}
1918

2019
// 反转琏表
21-
func printListFromTailToHead2(head *NodeList) []int {
22-
pre, cur, next, ans := &NodeList{}, head, head.Next, []int{}
23-
20+
func printListFromTailToHead2(head *ListNode) []int {
21+
pre, cur, next, ans := &ListNode{}, head, head.Next, []int{}
2422
for cur != nil {
2523
next = cur.Next
2624
cur.Next = pre
2725

2826
pre = cur
2927
cur = next
3028
}
31-
3229
for pre.Next != nil {
3330
ans = append(ans, pre.Val)
3431
pre = pre.Next
3532
}
36-
3733
return ans
3834
}

0 commit comments

Comments
 (0)