Clearly Java solution


  • 0
    L
    public int rob(int[] nums) {
    	 if(nums==null || nums.length == 0){
    		 return 0;
    	 }
    	 int len = nums.length;
    	 if(len==1){
    		 return nums[0];
    	 }
    	 int preA1=0,preA2=0,preB1=0,preB2=0,temp;
    	 preA1 = nums[0];
    	 preA2 = Math.max(nums[0], nums[1]);
    	 preB2 = nums[1];
    	 for(int i=2; i<len; ++i){
    		 temp = preA2;
    		 preA2 = Math.max(nums[i]+preA1, preA2);
    		 preA1 = temp;
    		 temp = preB2;
    		 preB2 = Math.max(nums[i]+preB1, preB2);
    		 preB1 = temp;
    	 }
    	 return Math.max(preA1, preB2);
     }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.