Skip to content

Commit a249f00

Browse files
committed
feat: update go solutions to lcof problems
1 parent 8730451 commit a249f00

File tree

18 files changed

+248
-250
lines changed

18 files changed

+248
-250
lines changed

lcof/面试题06. 从尾到头打印链表/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ class Solution {
7171
* Next *ListNode
7272
* }
7373
*/
74-
//insert to the front
74+
//insert to the front
7575
func reversePrint(head *ListNode) []int {
76-
res := []int{}
77-
for head != nil {
78-
res = append([]int{head.Val}, res...)
79-
head = head.Next
80-
}
81-
return res
76+
res := []int{}
77+
for head != nil {
78+
res = append([]int{head.Val}, res...)
79+
head = head.Next
80+
}
81+
return res
8282
}
8383
```
8484

lcof/面试题06. 从尾到头打印链表/Solution.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* Next *ListNode
66
* }
77
*/
8-
//insert to the front
8+
//insert to the front
99
func reversePrint(head *ListNode) []int {
10-
res := []int{}
11-
for head != nil {
12-
res = append([]int{head.Val}, res...)
13-
head = head.Next
14-
}
15-
return res
10+
res := []int{}
11+
for head != nil {
12+
res = append([]int{head.Val}, res...)
13+
head = head.Next
14+
}
15+
return res
1616
}

lcof/面试题09. 用两个栈实现队列/README.md

+23-24
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,37 @@ CQueue.prototype.deleteHead = function() {
130130

131131
```go
132132
type CQueue struct {
133-
Stack1 []int
134-
Stack2 []int
133+
Stack1 []int
134+
Stack2 []int
135135
}
136-
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶
137-
//否则,S1的元素从栈顶依次入S2,再从S2弹出
136+
137+
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
138+
//再从S2弹出
138139

139140
func Constructor() CQueue {
140-
return CQueue{Stack1:[]int{},Stack2:[]int{}}
141+
return CQueue{Stack1: []int{}, Stack2: []int{}}
141142
}
142143

143-
144-
func (this *CQueue) AppendTail(value int) {
145-
this.Stack1 = append(this.Stack1, value)
144+
func (this *CQueue) AppendTail(value int) {
145+
this.Stack1 = append(this.Stack1, value)
146146
}
147147

148-
149148
func (this *CQueue) DeleteHead() int {
150-
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
151-
return -1
152-
}
153-
if len(this.Stack2) > 0 {
154-
res := this.Stack2[len(this.Stack2)-1]
155-
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
156-
return res
157-
}
158-
for len(this.Stack1) > 0 {
159-
this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1])
160-
this.Stack1 = this.Stack1[0:len(this.Stack1)-1]
161-
}
162-
res := this.Stack2[len(this.Stack2)-1]
163-
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
164-
return res
149+
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
150+
return -1
151+
}
152+
if len(this.Stack2) > 0 {
153+
res := this.Stack2[len(this.Stack2)-1]
154+
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
155+
return res
156+
}
157+
for len(this.Stack1) > 0 {
158+
this.Stack2 = append(this.Stack2, this.Stack1[len(this.Stack1)-1])
159+
this.Stack1 = this.Stack1[0 : len(this.Stack1)-1]
160+
}
161+
res := this.Stack2[len(this.Stack2)-1]
162+
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
163+
return res
165164
}
166165
```
167166

Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
type CQueue struct {
2-
Stack1 []int
3-
Stack2 []int
2+
Stack1 []int
3+
Stack2 []int
44
}
5+
56
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
67
//再从S2弹出
78

89
func Constructor() CQueue {
9-
return CQueue{Stack1:[]int{},Stack2:[]int{}}
10+
return CQueue{Stack1: []int{}, Stack2: []int{}}
1011
}
1112

12-
13-
func (this *CQueue) AppendTail(value int) {
14-
this.Stack1 = append(this.Stack1, value)
13+
func (this *CQueue) AppendTail(value int) {
14+
this.Stack1 = append(this.Stack1, value)
1515
}
1616

17-
1817
func (this *CQueue) DeleteHead() int {
19-
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
20-
return -1
21-
}
22-
if len(this.Stack2) > 0 {
23-
res := this.Stack2[len(this.Stack2)-1]
24-
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
25-
return res
26-
}
27-
for len(this.Stack1) > 0 {
28-
this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1])
29-
this.Stack1 = this.Stack1[0:len(this.Stack1)-1]
30-
}
31-
res := this.Stack2[len(this.Stack2)-1]
32-
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
33-
return res
18+
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
19+
return -1
20+
}
21+
if len(this.Stack2) > 0 {
22+
res := this.Stack2[len(this.Stack2)-1]
23+
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
24+
return res
25+
}
26+
for len(this.Stack1) > 0 {
27+
this.Stack2 = append(this.Stack2, this.Stack1[len(this.Stack1)-1])
28+
this.Stack1 = this.Stack1[0 : len(this.Stack1)-1]
29+
}
30+
res := this.Stack2[len(this.Stack2)-1]
31+
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
32+
return res
3433
}

lcof/面试题10- II. 青蛙跳台阶问题/README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@ var numWays = function(n) {
8080

8181
```go
8282
func numWays(n int) int {
83-
if n == 0 {
84-
return 1
85-
}
86-
if n <= 2 {
87-
return n
88-
}
89-
a := make([]int,n)
90-
a[0] = 1
91-
a[1] = 2
92-
for i := 2; i<n; i++ {
93-
a[i] = (a[i-1]+a[i-2])%1000000007
94-
}
95-
return a[n-1]
83+
if n == 0 {
84+
return 1
85+
}
86+
if n <= 2 {
87+
return n
88+
}
89+
a := make([]int, n)
90+
a[0] = 1
91+
a[1] = 2
92+
for i := 2; i < n; i++ {
93+
a[i] = (a[i-1] + a[i-2]) % 1000000007
94+
}
95+
return a[n-1]
9696
}
9797
```
9898

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
func numWays(n int) int {
2-
if n == 0 {
3-
return 1
4-
}
5-
if n <= 2 {
6-
return n
7-
}
8-
a := make([]int,n)
9-
a[0] = 1
10-
a[1] = 2
11-
for i := 2; i<n; i++ {
12-
a[i] = (a[i-1]+a[i-2])%1000000007
13-
}
14-
return a[n-1]
2+
if n == 0 {
3+
return 1
4+
}
5+
if n <= 2 {
6+
return n
7+
}
8+
a := make([]int, n)
9+
a[0] = 1
10+
a[1] = 2
11+
for i := 2; i < n; i++ {
12+
a[i] = (a[i-1] + a[i-2]) % 1000000007
13+
}
14+
return a[n-1]
1515
}

lcof/面试题11. 旋转数组的最小数字/README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ var minArray = function(numbers) {
102102

103103
```go
104104
func minArray(nums []int) int {
105-
l,r := 0,len(nums)-1
106-
for l < r {
107-
mid := l + (r-l)>>1
108-
if nums[mid] > nums[r] {
109-
l = mid + 1
110-
} else if nums[mid] <nums[r] {
111-
r = mid //r 本身不需要被排除
112-
} else {
113-
r--
114-
}
115-
}
116-
return nums[l]
105+
l, r := 0, len(nums)-1
106+
for l < r {
107+
mid := l + (r-l)>>1
108+
if nums[mid] > nums[r] {
109+
l = mid + 1
110+
} else if nums[mid] < nums[r] {
111+
r = mid //r 本身不需要被排除
112+
} else {
113+
r--
114+
}
115+
}
116+
return nums[l]
117117
}
118118
```
119119

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
func minArray(nums []int) int {
2-
l,r := 0,len(nums)-1
3-
for l < r {
4-
mid := l + (r-l)>>1
5-
if nums[mid] > nums[r] {
6-
l = mid + 1
7-
} else if nums[mid] <nums[r] {
8-
r = mid //r 本身不需要被排除
9-
} else {
10-
r--
11-
}
12-
}
13-
return nums[l]
2+
l, r := 0, len(nums)-1
3+
for l < r {
4+
mid := l + (r-l)>>1
5+
if nums[mid] > nums[r] {
6+
l = mid + 1
7+
} else if nums[mid] < nums[r] {
8+
r = mid //r 本身不需要被排除
9+
} else {
10+
r--
11+
}
12+
}
13+
return nums[l]
1414
}

lcof/面试题12. 矩阵中的路径/README.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -144,40 +144,40 @@ var exist = function(board, word) {
144144

145145
```go
146146
func exist(board [][]byte, word string) bool {
147-
if len(board) == 0 {
148-
return false
149-
}
150-
//标记数组
151-
isVisited := make([][]bool,len(board))
152-
for i := 0; i < len(board); i++ {
153-
isVisited[i] = make([]bool, len(board[0]))
154-
}
155-
for i := 0; i < len(board); i++ {
156-
for j := 0; j < len(board[0]); j++ {
157-
if board[i][j] == word[0] {
158-
if bfs(board,i,j,isVisited,word,0) {
159-
return true
160-
}
161-
}
162-
}
163-
}
164-
return false
147+
if len(board) == 0 {
148+
return false
149+
}
150+
//标记数组
151+
isVisited := make([][]bool, len(board))
152+
for i := 0; i < len(board); i++ {
153+
isVisited[i] = make([]bool, len(board[0]))
154+
}
155+
for i := 0; i < len(board); i++ {
156+
for j := 0; j < len(board[0]); j++ {
157+
if board[i][j] == word[0] {
158+
if bfs(board, i, j, isVisited, word, 0) {
159+
return true
160+
}
161+
}
162+
}
163+
}
164+
return false
165165
}
166166

167-
func bfs(board [][]byte, i,j int, isVisited [][]bool, word string, index int) bool {
168-
if index == len(word) {
169-
return true
170-
}
171-
if i < 0 || j < 0 || i == len(board) || j == len(board[0]) || isVisited[i][j] || board[i][j] != word[index] {
172-
return false
173-
}
174-
isVisited[i][j] = true
175-
res := bfs(board, i+1, j, isVisited, word, index+1) ||
176-
bfs(board, i, j+1, isVisited, word, index+1) ||
177-
bfs(board, i-1, j, isVisited, word, index+1) ||
178-
bfs(board, i, j-1, isVisited, word, index+1)
179-
isVisited[i][j] = false
180-
return res
167+
func bfs(board [][]byte, i, j int, isVisited [][]bool, word string, index int) bool {
168+
if index == len(word) {
169+
return true
170+
}
171+
if i < 0 || j < 0 || i == len(board) || j == len(board[0]) || isVisited[i][j] || board[i][j] != word[index] {
172+
return false
173+
}
174+
isVisited[i][j] = true
175+
res := bfs(board, i+1, j, isVisited, word, index+1) ||
176+
bfs(board, i, j+1, isVisited, word, index+1) ||
177+
bfs(board, i-1, j, isVisited, word, index+1) ||
178+
bfs(board, i, j-1, isVisited, word, index+1)
179+
isVisited[i][j] = false
180+
return res
181181
}
182182
```
183183

0 commit comments

Comments
 (0)