198

class Solution {
    public int rob(int[] nums) {
        int len = nums.length;
        if(0 == len){
            return 0;
        }
        int[] dp = new int[len + 1];
        dp[0] = 0;
        dp[1] = nums[0];
        for(int i = 2; i <= len; i++){
            dp[i] = Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]);
        }
        return dp[len];
    }
}

 

转载于:https://www.cnblogs.com/zhaijiayu/p/11557460.html

你可能感兴趣的:(198)