Skip to content

Commit aa9d559

Browse files
authored
Create sourcema.md
1 parent 36caf5b commit aa9d559

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

2019.01.28-leetcode213/sourcema.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# leetcode 213
2+
class Solution {
3+
public int rob(int[] nums) {
4+
if(nums==null||nums.length==0){
5+
return 0;
6+
}
7+
if(nums.length==1){
8+
return nums[0];
9+
}
10+
if(nums.length==2){
11+
return Math.max(nums[0],nums[1]);
12+
}
13+
int[] dp1=new int[nums.length];
14+
int[] dp2=new int[nums.length-1];
15+
dp1[1]=nums[1];
16+
dp2[0]=nums[0];dp2[1]=Math.max(dp2[0],nums[1]);
17+
for(int i=2;i<nums.length;i++){//不包含第一个
18+
dp1[i]=Math.max(dp1[i-1],dp1[i-2]+nums[i]);
19+
}
20+
for(int i=2;i<nums.length-1;i++){
21+
dp2[i]=Math.max(dp2[i-1],dp2[i-2]+nums[i]);
22+
}
23+
return Math.max(dp1[dp1.length-1],dp2[dp2.length-1]);
24+
}
25+
}

0 commit comments

Comments
 (0)