题解 | 打家劫舍(一)
打家劫舍(一)
https://www.nowcoder.com/practice/c5fbf7325fbd4c0ea3d0c3ea6bc6cc79
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int rob (int[] nums) { // write code here int dp = nums[0], dp0 = 0 , dp1 = nums[0]; for(int i = 2; i <= nums.length; i++){ dp = Math.max(nums[i-1] + dp0,dp1); dp0 = dp1; dp1 = dp; } return dp; } }