Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 《巨蟒》中的强盗_Python - Fatal编程技术网

Python 《巨蟒》中的强盗

Python 《巨蟒》中的强盗,python,Python,我正在浏览leetcode上的解决方案,并为HouseRobber 1找到了以下内容: last, now = 0, 0 for i in nums: last, now = now, max(last + i, now) return now 我对代码本身如何使用最大值执行有点困惑。这对我来说有点模棱两可,因为我对Python还是相当新的,而且来自C++,所以我写了这样的东西: lastNum = 0 #last number we took in

我正在浏览leetcode上的解决方案,并为HouseRobber 1找到了以下内容:

    last, now = 0, 0
    for i in nums:
        last, now = now, max(last + i, now)
    return now
我对代码本身如何使用最大值执行有点困惑。这对我来说有点模棱两可,因为我对Python还是相当新的,而且来自C++,所以我写了这样的东西:

lastNum = 0 #last number we took in
    lastSpot = 0 #last spot we took the number from (index placeholder)
    totalSum = 0 #max amount we can take from the houses
    for i, element in enumerate(nums):
        if element > lastNum: #if our element is bigger than our last number, we check...
            if lastSpot != i - 1: #if our last spot was more than a space away AND our element is bigger, we take our index number.
                if lastNum + nums[lastSpot] < lastNum + nums[i]:
                    lastNum = element
                    totalSum += element
                    lastSpot = i #update our last spot
            elif lastSpot == i - 1: #if our last spot is next to our current spot, we gotta' swap a few values now.
                totalSum -= lastNum #we remove the last value we added since we found a bigger spot
                totalSum += element
                lastSpot = i
                lastNum = element
        elif i == lastSpot + 2: #if we are more than one spot away from our last spot, we take the number anyway and add it.
            lastNum = element
            totalSum += element
        elif element < lastNum:
            if lastNum + nums[lastSpot] < lastNum + nums[i]:
                lastNum = element
                totalSum += element
                totalSum += nums[i - 2]
                lastSpot = i        
        else:   
            lastNum = element

    return totalSum
lastNum=0#我们接收的最后一个号码
lastSpot=0#我们从中获取数字的最后一个点(索引占位符)
totalSum=0#我们可以从房子里拿走的最大数量
对于i,枚举中的元素(nums):
if element>lastNum:#如果我们的元素大于上一个数字,我们会检查。。。
如果最后一点!=i-1:#如果我们的最后一个点距离一个多空间,并且我们的元素更大,那么我们取我们的索引号。
如果lastNum+nums[lastpot]

我知道我们需要相互验证这些值,如果我们传递了一个值,我们会检查它是否大于我们上次持有的值加上当前值,然后根据它是否是我们当前选择的值旁边的位置决定跳过或接受。但是在一小段python代码中如何执行呢?我知道max函数是如何取2个值并返回两个值中的较大值的——但我是否遗漏了什么?这不也应该返回相同值旁边的值吗?谢谢你抽出时间

在这段代码中,对每个迭代执行如下操作:

temp = last
last = now
now = max(temp + i, now)
所以C++中的代码看起来像:

last = 0;
now = 0;

for (int i = 0; i < num.length; i++) {
    temp = last;
    last = now;
    now = max(temp + nums[i], now);
}
last=0;
现在=0;
for(int i=0;i
你的主要问题是关于
last,now=now,max(last+i,now)
如何工作?“相同值旁边的值”是什么意思?您的第一个块是正确的,但我认为您意外地将
now=max(last+I,now)
而不是
now=max(temp+I,now)
放入循环中。其中的
i
应该是
num[i]
,因为它是nums中i的
。很抱歉吹毛求疵。@Bakhromarkhmonov好的,那么本质上我们是在迭代,然后使用最大值检查当前值和索引项中的值+1之间的最大值?是吗?是的,你是对的。这是一个简单的dp问题。但不是价值+1,最后价值+当前房款