Skip to content

Commit 03026d6

Browse files
authored
Merge pull request doocs#204 from lightfish-zhang/master
add golang Solution for problem 213
2 parents 4476ec2 + e74483b commit 03026d6

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

solution/0198.House Robber/Solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
func rob(nums []int) int {
77
x, y := 0, 0
88
for _, n := range nums {
9-
x, y = y, x+y
9+
x, y = y, x+n
1010
if x > y {
1111
y = x
1212
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func rob(nums []int) int {
2+
l := len(nums)
3+
if l == 0 {
4+
return 0
5+
}
6+
if l == 1 {
7+
return nums[0]
8+
}
9+
res1 := _rob(nums[:l-1])
10+
res2 := _rob(nums[1:])
11+
if res1 < res2 {
12+
return res2
13+
}
14+
return res1
15+
}
16+
17+
func _rob(nums []int) int {
18+
x, y := 0, 0
19+
for _, n := range nums {
20+
x, y = y, x+n
21+
if x > y {
22+
y = x
23+
}
24+
}
25+
return y
26+
}

0 commit comments

Comments
 (0)