题目
https://leetcode-cn.com/problems/house-robber/
PHP代码
class Solution
{
/**
* @param Integer[] $nums
* @return Integer
*/
public function rob($nums)
{
//$result=max($this->digui($k-1,$nums), $this->digui($k - 2,$nums) + $nums[$k - 1]);
$lenth = sizeof($nums);
$pre1 = $nums[0];
$pre2 = 0;
for ($k = 2; $k <= $lenth; $k++) {
$result = max($pre1, $pre2 + $nums[$k - 1]);
$pre2 = $pre1;
$pre1 = $result;
}
return $pre1;
}
}