LeetCode 198. House Robber [Easy]

LeetCode 198. House Robber.

题目来源:https://leetcode.com/problems/house-robber

题目难度:Easy

解答1[Java]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int rob(int[] nums) {
if (nums.length == 0)
return 0;
int prev1 = 0;
int prev2 = 0;
for (int num : nums) {
int tmp = prev1;
prev1 = Math.max(prev2 + num, prev1);
prev2 = tmp;
}
return prev1;
}
}