Skip to content

Commit eb6ffd4

Browse files
authored
Merge pull request #203 from lightfish-zhang/master
add Solution 70,198 for golang
2 parents a301155 + baa6a72 commit eb6ffd4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func climbStairs(n int) int {
2+
if n < 3 {
3+
return n
4+
}
5+
x, y := 1, 2
6+
for i := 2; i < n; i++ {
7+
x, y = y, x+y
8+
}
9+
return y
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
198.House Robber
3+
动态规划的思路,找到状态转移方程 `f(n) = max(f(n-2)+nums[n], f(n-1))` ,初始值为 `f(0)=0, f(1)=nums[1]`
4+
*/
5+
6+
func rob(nums []int) int {
7+
x, y := 0, 0
8+
for _, n := range nums {
9+
x, y = y, x+y
10+
if x > y {
11+
y = x
12+
}
13+
}
14+
return y
15+
}

0 commit comments

Comments
 (0)