Skip to content
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

feat: add Golang solution for lcof problem 10-11 #272

Merged
merged 1 commit into from
Jul 4, 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
feat: add Golang solution for lcof problem 10-11
  • Loading branch information
hzliangbin committed Jul 4, 2020
commit c09abd75f4971bf7eec3befe75ce6d18aa541604
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]
}