You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.題目的意思是,咱們是一個江洋大盜~如今咱們要去偷整條街的房子,每一個房子裏有必定的錢。可是任何臨近的兩個房子被偷就會觸發警報。要求咱們求出在不觸發警報的狀況下偷到的最多的錢。每一個房子裏的錢經過輸入的int數組表示。數組
public int rob(int[] nums) { int prevNo = 0; int prevYes = 0; for(int n: nums){ int temp = prevNo; prevNo = Math.max(prevNo, prevYes); prevYes = temp+n; } return Math.max(prevNo, prevYes); }