Skip to content

Commit 4b09b1f

Browse files
committed
feat:add golang solution for lcof problem 06 07 09
1 parent f60c8d4 commit 4b09b1f

File tree

6 files changed

+168
-3
lines changed

6 files changed

+168
-3
lines changed

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,24 @@ class Solution {
6060
}
6161
```
6262

63-
### JavaScript
64-
```js
65-
63+
### Go
64+
```go
65+
/**
66+
* Definition for singly-linked list.
67+
* type ListNode struct {
68+
* Val int
69+
* Next *ListNode
70+
* }
71+
*/
72+
//insert to the front
73+
func reversePrint(head *ListNode) []int {
74+
res := []int{}
75+
for head != nil {
76+
res = append([]int{head.Val}, res...)
77+
head = head.Next
78+
}
79+
return res
80+
}
6681
```
6782

6883
### ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
//insert to the front
9+
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
16+
}

lcof/面试题07. 重建二叉树/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,40 @@ var buildTree = function(preorder, inorder) {
136136
};
137137
```
138138

139+
### Go
140+
141+
```go
142+
/**
143+
* Definition for a binary tree node.
144+
* type TreeNode struct {
145+
* Val int
146+
* Left *TreeNode
147+
* Right *TreeNode
148+
* }
149+
*/
150+
func buildTree(preorder []int, inorder []int) *TreeNode {
151+
return helper(preorder, inorder, 0, 0, len(preorder)-1)
152+
}
153+
154+
func helper(preorder, inorder []int, index, start, end int) *TreeNode {
155+
if start > end {
156+
return nil
157+
}
158+
root := &TreeNode{Val:preorder[index]}
159+
j := start
160+
for j < end && preorder[index] != inorder[j] {
161+
j++
162+
}
163+
root.Left = helper(preorder, inorder, index + 1, start, j - 1)
164+
root.Right = helper(preorder, inorder, index + 1 + j -start, j + 1, end)
165+
return root
166+
}
167+
```
168+
169+
170+
139171
### ...
172+
140173
```
141174
142175
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func buildTree(preorder []int, inorder []int) *TreeNode {
10+
return helper(preorder, inorder, 0, 0, len(preorder)-1)
11+
}
12+
13+
func helper(preorder, inorder []int, index, start, end int) *TreeNode {
14+
if start > end {
15+
return nil
16+
}
17+
root := &TreeNode{Val:preorder[index]}
18+
j := start
19+
for j < end && preorder[index] != inorder[j] {
20+
j++
21+
}
22+
root.Left = helper(preorder, inorder, index + 1, start, j - 1)
23+
root.Right = helper(preorder, inorder, index + 1 + j -start, j + 1, end)
24+
return root
25+
}

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

+42
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,49 @@ CQueue.prototype.deleteHead = function() {
124124
};
125125
```
126126

127+
### Go
128+
129+
```go
130+
type CQueue struct {
131+
Stack1 []int
132+
Stack2 []int
133+
}
134+
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶
135+
//否则,S1的元素从栈顶依次入S2,再从S2弹出
136+
137+
func Constructor() CQueue {
138+
return CQueue{Stack1:[]int{},Stack2:[]int{}}
139+
}
140+
141+
142+
func (this *CQueue) AppendTail(value int) {
143+
this.Stack1 = append(this.Stack1, value)
144+
}
145+
146+
147+
func (this *CQueue) DeleteHead() int {
148+
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
149+
return -1
150+
}
151+
if len(this.Stack2) > 0 {
152+
res := this.Stack2[len(this.Stack2)-1]
153+
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
154+
return res
155+
}
156+
for len(this.Stack1) > 0 {
157+
this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1])
158+
this.Stack1 = this.Stack1[0:len(this.Stack1)-1]
159+
}
160+
res := this.Stack2[len(this.Stack2)-1]
161+
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
162+
return res
163+
}
164+
```
165+
166+
167+
127168
### ...
169+
128170
```
129171
130172
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
type CQueue struct {
2+
Stack1 []int
3+
Stack2 []int
4+
}
5+
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
6+
//再从S2弹出
7+
8+
func Constructor() CQueue {
9+
return CQueue{Stack1:[]int{},Stack2:[]int{}}
10+
}
11+
12+
13+
func (this *CQueue) AppendTail(value int) {
14+
this.Stack1 = append(this.Stack1, value)
15+
}
16+
17+
18+
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
34+
}

0 commit comments

Comments
 (0)