LeetCode198.md

题目


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

}

测试结果

LeetCode198.md

上一篇:LeetCode70. 爬楼梯Golang版


下一篇:求最小步变成斐波拉契数