We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 36caf5b commit aa9d559Copy full SHA for aa9d559
2019.01.28-leetcode213/sourcema.md
@@ -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