Skip to content

Commit f87ba6e

Browse files
author
Kyle Liu
authored
Merge pull request #175 from kylesliu/develop
add some solution
2 parents 1ae0759 + e8413c3 commit f87ba6e

23 files changed

+834
-6
lines changed

lcof/of006/Solution.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ type ListNode struct {
66
}
77

88
// 递归写法
9-
func printListFromTailToHead(head *ListNode) []int {
9+
func reversePrint(head *ListNode) []int {
1010
ans := make([]int, 0)
1111
if head == nil {
1212
return ans
1313
}
14-
ans = printListFromTailToHead(head.Next)
14+
ans = reversePrint(head.Next)
1515
ans = append(ans, head.Val)
1616
return ans
1717
}
1818

1919
// 反转琏表
20-
func printListFromTailToHead2(head *ListNode) []int {
20+
func reversePrint2(head *ListNode) []int {
21+
if head == nil {
22+
return []int{}
23+
}
2124
pre, cur, next, ans := &ListNode{}, head, head.Next, []int{}
2225
for cur != nil {
2326
next = cur.Next

lcof/of006/Solution_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
type SolutionFuncType func(node *ListNode) []int
1212

1313
var SolutionFuncList = []SolutionFuncType{
14-
printListFromTailToHead,
15-
printListFromTailToHead2,
14+
reversePrint,
15+
reversePrint2,
1616
}
1717

1818
// test info struct
@@ -25,7 +25,12 @@ type Case struct {
2525
// test case
2626
var cases = []Case{
2727
{
28-
name: "TestCase 1",
28+
name: "TestCase 1",
29+
inputs: nil,
30+
expect: []int{},
31+
},
32+
{
33+
name: "TestCase 2",
2934
inputs: &ListNode{Val: 1,
3035
Next: &ListNode{Val: 2, Next: &ListNode{Val: 3}}},
3136
expect: []int{3, 2, 1},

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Solution
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func deleteNode(head *ListNode, val int) *ListNode {
9+
if head == nil {
10+
return nil
11+
} else if head.Val == val {
12+
return head.Next
13+
}
14+
15+
ans := head
16+
for head.Next.Val != val {
17+
head = head.Next
18+
}
19+
head.Next = head.Next.Next
20+
return ans
21+
}

lcof/of018/Solution_test.go

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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Solution
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func getKthFromEnd(head *ListNode, k int) *ListNode {
9+
left, right := head, head
10+
for i := 0; i < k; i++ {
11+
right = right.Next
12+
}
13+
for right != nil {
14+
right = right.Next
15+
left = left.Next
16+
}
17+
return left
18+
}

lcof/of022/Solution_test.go

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

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Solution
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func reverseList(head *ListNode) *ListNode {
9+
if head == nil || head.Next == nil {
10+
return head
11+
}
12+
newHead := reverseList(head.Next)
13+
head.Next.Next = head
14+
head.Next = nil
15+
return newHead
16+
}
17+
18+
//func reverseList2(head *ListNode) *ListNode {
19+
// var pre *ListNode
20+
// var next *ListNode
21+
// for head != nil {
22+
// next = head.Next
23+
// head.Next = pre
24+
// pre = head
25+
// head = next
26+
// }
27+
// return pre
28+
//}
29+
30+
//func reverseList3(head *ListNode) *ListNode {
31+
// var prev *ListNode
32+
//
33+
// for head != nil {
34+
// head.Next, head, prev = prev, head.Next, head
35+
// }
36+
// return prev
37+
//}

0 commit comments

Comments
 (0)