Skip to content

feat:add golang solution for lcof problem 06 07 09 #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lcof/面试题06. 从尾到头打印链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,24 @@ class Solution {
}
```

### JavaScript
```js

### Go
```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
//insert to the front
func reversePrint(head *ListNode) []int {
res := []int{}
for head != nil {
res = append([]int{head.Val}, res...)
head = head.Next
}
return res
}
```

### ...
Expand Down
16 changes: 16 additions & 0 deletions lcof/面试题06. 从尾到头打印链表/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
//insert to the front
func reversePrint(head *ListNode) []int {
res := []int{}
for head != nil {
res = append([]int{head.Val}, res...)
head = head.Next
}
return res
}
33 changes: 33 additions & 0 deletions lcof/面试题07. 重建二叉树/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,40 @@ var buildTree = function(preorder, inorder) {
};
```

### Go

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func buildTree(preorder []int, inorder []int) *TreeNode {
return helper(preorder, inorder, 0, 0, len(preorder)-1)
}

func helper(preorder, inorder []int, index, start, end int) *TreeNode {
if start > end {
return nil
}
root := &TreeNode{Val:preorder[index]}
j := start
for j < end && preorder[index] != inorder[j] {
j++
}
root.Left = helper(preorder, inorder, index + 1, start, j - 1)
root.Right = helper(preorder, inorder, index + 1 + j -start, j + 1, end)
return root
}
```



### ...

```

```
25 changes: 25 additions & 0 deletions lcof/面试题07. 重建二叉树/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func buildTree(preorder []int, inorder []int) *TreeNode {
return helper(preorder, inorder, 0, 0, len(preorder)-1)
}

func helper(preorder, inorder []int, index, start, end int) *TreeNode {
if start > end {
return nil
}
root := &TreeNode{Val:preorder[index]}
j := start
for j < end && preorder[index] != inorder[j] {
j++
}
root.Left = helper(preorder, inorder, index + 1, start, j - 1)
root.Right = helper(preorder, inorder, index + 1 + j -start, j + 1, end)
return root
}
42 changes: 42 additions & 0 deletions lcof/面试题09. 用两个栈实现队列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,49 @@ CQueue.prototype.deleteHead = function() {
};
```

### Go

```go
type CQueue struct {
Stack1 []int
Stack2 []int
}
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶
//否则,S1的元素从栈顶依次入S2,再从S2弹出

func Constructor() CQueue {
return CQueue{Stack1:[]int{},Stack2:[]int{}}
}


func (this *CQueue) AppendTail(value int) {
this.Stack1 = append(this.Stack1, value)
}


func (this *CQueue) DeleteHead() int {
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
return -1
}
if len(this.Stack2) > 0 {
res := this.Stack2[len(this.Stack2)-1]
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
return res
}
for len(this.Stack1) > 0 {
this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1])
this.Stack1 = this.Stack1[0:len(this.Stack1)-1]
}
res := this.Stack2[len(this.Stack2)-1]
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
return res
}
```



### ...

```

```
34 changes: 34 additions & 0 deletions lcof/面试题09. 用两个栈实现队列/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type CQueue struct {
Stack1 []int
Stack2 []int
}
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
//再从S2弹出

func Constructor() CQueue {
return CQueue{Stack1:[]int{},Stack2:[]int{}}
}


func (this *CQueue) AppendTail(value int) {
this.Stack1 = append(this.Stack1, value)
}


func (this *CQueue) DeleteHead() int {
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
return -1
}
if len(this.Stack2) > 0 {
res := this.Stack2[len(this.Stack2)-1]
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
return res
}
for len(this.Stack1) > 0 {
this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1])
this.Stack1 = this.Stack1[0:len(this.Stack1)-1]
}
res := this.Stack2[len(this.Stack2)-1]
this.Stack2 = this.Stack2[0:len(this.Stack2)-1]
return res
}