Skip to content

Commit

Permalink
feat: update go solutions to lcof problems
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Oct 3, 2020
1 parent 8730451 commit a249f00
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 250 deletions.
14 changes: 7 additions & 7 deletions lcof/面试题06. 从尾到头打印链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ class Solution {
* Next *ListNode
* }
*/
//insert to the front
//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
res := []int{}
for head != nil {
res = append([]int{head.Val}, res...)
head = head.Next
}
return res
}
```

Expand Down
14 changes: 7 additions & 7 deletions lcof/面试题06. 从尾到头打印链表/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* Next *ListNode
* }
*/
//insert to the front
//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
res := []int{}
for head != nil {
res = append([]int{head.Val}, res...)
head = head.Next
}
return res
}
47 changes: 23 additions & 24 deletions lcof/面试题09. 用两个栈实现队列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,37 @@ CQueue.prototype.deleteHead = function() {

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

// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
//再从S2弹出

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


func (this *CQueue) AppendTail(value int) {
this.Stack1 = append(this.Stack1, value)
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
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
}
```

Expand Down
43 changes: 21 additions & 22 deletions lcof/面试题09. 用两个栈实现队列/Solution.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
type CQueue struct {
Stack1 []int
Stack2 []int
Stack1 []int
Stack2 []int
}

// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
//再从S2弹出

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


func (this *CQueue) AppendTail(value int) {
this.Stack1 = append(this.Stack1, value)
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
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
}
26 changes: 13 additions & 13 deletions lcof/面试题10- II. 青蛙跳台阶问题/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ var numWays = function(n) {

```go
func numWays(n int) int {
if n == 0 {
return 1
}
if n <= 2 {
return n
}
a := make([]int,n)
a[0] = 1
a[1] = 2
for i := 2; i<n; i++ {
a[i] = (a[i-1]+a[i-2])%1000000007
}
return a[n-1]
if n == 0 {
return 1
}
if n <= 2 {
return n
}
a := make([]int, n)
a[0] = 1
a[1] = 2
for i := 2; i < n; i++ {
a[i] = (a[i-1] + a[i-2]) % 1000000007
}
return a[n-1]
}
```

Expand Down
26 changes: 13 additions & 13 deletions lcof/面试题10- II. 青蛙跳台阶问题/Solution.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
func numWays(n int) int {
if n == 0 {
return 1
}
if n <= 2 {
return n
}
a := make([]int,n)
a[0] = 1
a[1] = 2
for i := 2; i<n; i++ {
a[i] = (a[i-1]+a[i-2])%1000000007
}
return a[n-1]
if n == 0 {
return 1
}
if n <= 2 {
return n
}
a := make([]int, n)
a[0] = 1
a[1] = 2
for i := 2; i < n; i++ {
a[i] = (a[i-1] + a[i-2]) % 1000000007
}
return a[n-1]
}
24 changes: 12 additions & 12 deletions lcof/面试题11. 旋转数组的最小数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ var minArray = function(numbers) {

```go
func minArray(nums []int) int {
l,r := 0,len(nums)-1
for l < r {
mid := l + (r-l)>>1
if nums[mid] > nums[r] {
l = mid + 1
} else if nums[mid] <nums[r] {
r = mid //r 本身不需要被排除
} else {
r--
}
}
return nums[l]
l, r := 0, len(nums)-1
for l < r {
mid := l + (r-l)>>1
if nums[mid] > nums[r] {
l = mid + 1
} else if nums[mid] < nums[r] {
r = mid //r 本身不需要被排除
} else {
r--
}
}
return nums[l]
}
```

Expand Down
24 changes: 12 additions & 12 deletions lcof/面试题11. 旋转数组的最小数字/Solution.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
func minArray(nums []int) int {
l,r := 0,len(nums)-1
for l < r {
mid := l + (r-l)>>1
if nums[mid] > nums[r] {
l = mid + 1
} else if nums[mid] <nums[r] {
r = mid //r 本身不需要被排除
} else {
r--
}
}
return nums[l]
l, r := 0, len(nums)-1
for l < r {
mid := l + (r-l)>>1
if nums[mid] > nums[r] {
l = mid + 1
} else if nums[mid] < nums[r] {
r = mid //r 本身不需要被排除
} else {
r--
}
}
return nums[l]
}
64 changes: 32 additions & 32 deletions lcof/面试题12. 矩阵中的路径/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,40 +144,40 @@ var exist = function(board, word) {

```go
func exist(board [][]byte, word string) bool {
if len(board) == 0 {
return false
}
//标记数组
isVisited := make([][]bool,len(board))
for i := 0; i < len(board); i++ {
isVisited[i] = make([]bool, len(board[0]))
}
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[0]); j++ {
if board[i][j] == word[0] {
if bfs(board,i,j,isVisited,word,0) {
return true
}
}
}
}
return false
if len(board) == 0 {
return false
}
//标记数组
isVisited := make([][]bool, len(board))
for i := 0; i < len(board); i++ {
isVisited[i] = make([]bool, len(board[0]))
}
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[0]); j++ {
if board[i][j] == word[0] {
if bfs(board, i, j, isVisited, word, 0) {
return true
}
}
}
}
return false
}

func bfs(board [][]byte, i,j int, isVisited [][]bool, word string, index int) bool {
if index == len(word) {
return true
}
if i < 0 || j < 0 || i == len(board) || j == len(board[0]) || isVisited[i][j] || board[i][j] != word[index] {
return false
}
isVisited[i][j] = true
res := bfs(board, i+1, j, isVisited, word, index+1) ||
bfs(board, i, j+1, isVisited, word, index+1) ||
bfs(board, i-1, j, isVisited, word, index+1) ||
bfs(board, i, j-1, isVisited, word, index+1)
isVisited[i][j] = false
return res
func bfs(board [][]byte, i, j int, isVisited [][]bool, word string, index int) bool {
if index == len(word) {
return true
}
if i < 0 || j < 0 || i == len(board) || j == len(board[0]) || isVisited[i][j] || board[i][j] != word[index] {
return false
}
isVisited[i][j] = true
res := bfs(board, i+1, j, isVisited, word, index+1) ||
bfs(board, i, j+1, isVisited, word, index+1) ||
bfs(board, i-1, j, isVisited, word, index+1) ||
bfs(board, i, j-1, isVisited, word, index+1)
isVisited[i][j] = false
return res
}
```

Expand Down
Loading

0 comments on commit a249f00

Please sign in to comment.