Skip to content

Commit

Permalink
feat: add Golang solution for lcof problem 10-11
Browse files Browse the repository at this point in the history
  • Loading branch information
hzliangbin committed Jul 4, 2020
1 parent ec51232 commit c09abd7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lcof/面试题10- I. 斐波那契数列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,31 @@ var fib = function(n) {
};
```



### Go

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





### ...

```
```
12 changes: 12 additions & 0 deletions lcof/面试题10- I. 斐波那契数列/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func fib(n int) int {
if n < 2 {
return n
}
a := make([]int,n+1)
a[0]=0
a[1]=1
for i := 2; i < n+1; i++ {
a[i] = (a[i-1]+ a[i-2])%1000000007
}
return a[n]
}
25 changes: 25 additions & 0 deletions lcof/面试题10- II. 青蛙跳台阶问题/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,32 @@ var numWays = function(n) {
};
```



### Go

```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]
}
```



### ...

```
```
15 changes: 15 additions & 0 deletions lcof/面试题10- II. 青蛙跳台阶问题/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +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]
}
24 changes: 24 additions & 0 deletions lcof/面试题11. 旋转数组的最小数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,31 @@ var minArray = function(numbers) {
};
```

### Go

```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]
}
```





### ...

```
```
14 changes: 14 additions & 0 deletions lcof/面试题11. 旋转数组的最小数字/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +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]
}

0 comments on commit c09abd7

Please sign in to comment.