解包 * 在 leetcode 上抛出语法错误

我正在解决硬币找零问题。我使用 leetcode 上的给定示例在 jupyter-notebook 上运行代码,并且可以正常工作。

解包 * 在 leetcode 上抛出语法错误

同样的代码在 leetcode 上不起作用。导致语法错误:

解包 * 在 leetcode 上抛出语法错误

复制代码如下:

def best_sum(target,nums):
    dp=[None for y in range(target+1)]
    dp[0]=[]
    for i in range(len(dp)):
        if dp[i]!=None:
            for num in nums:
                if i+num<=target:
                    combination=[*dp[i],num]
                    if dp[i+num]==None or len(combination)<len(dp[i+num]):
                        dp[i+num]=combination
    return dp[-1]
best_sum(11,[1,2,5])
chenyaleiBB 回答:解包 * 在 leetcode 上抛出语法错误

将您的 LeetCode 语言设置为“Python 3”。解包操作符不是 Python 2 中的东西。

如果您不知道,LeetCode 上有两种语言 <div id="contents"> <h1>Hamster Adventure</h1> <button type='button' onclick='f1()'>Start</button> </div> <br> <div id="page"></div>python。显然 python3 指的是 Python 2.7。

本文链接:https://www.f2er.com/3182.html

大家都在问