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); }