其他分享
首页 > 其他分享> > LeetCode198.md

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

}

测试结果

在这里插入图片描述

标签:md,return,nums,LeetCode198,result,Integer,pre1,pre2
来源: https://blog.csdn.net/hahaha232323/article/details/114918863