Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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 Leetcode问题136。单个数字-如何修复代码,以及逻辑是否工作?_Python - Fatal编程技术网

Python Leetcode问题136。单个数字-如何修复代码,以及逻辑是否工作?

Python Leetcode问题136。单个数字-如何修复代码,以及逻辑是否工作?,python,Python,当我运行这段代码时,我得到一个错误:“AttributeError:'NoneType'对象没有属性'append'”。 我不明白为什么!也不知道该怎么办请帮忙 (也不知道是否可以使用.remove()) 谢谢,这是我的代码: class Solution: def singleNumber(self, nums: List[int]) -> int: stack = [] if len(nums) == 1:

当我运行这段代码时,我得到一个错误:“AttributeError:'NoneType'对象没有属性'append'”。 我不明白为什么!也不知道该怎么办请帮忙

(也不知道是否可以使用.remove())

谢谢,这是我的代码:

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        stack = []
        
        if len(nums) == 1:
            return nums
        
        i = 0
        while i < len(nums):
            if not stack:
                stack = stack.append(nums[i])
            elif stack and nums[i] not in stack:
                stack = stack.append(nums[i])
            elif nums[i] in stack[0]:
                stack = stack.remove(nums[i])
            i += 1
        return stack[0]
类解决方案:
def singleNumber(self,nums:List[int])->int:
堆栈=[]
如果len(nums)==1:
返回nums
i=0
而我
欢迎来到SO。请包含完整的回溯错误。也就是说,在每个
if
条件下,您都在覆盖
堆栈的内容
stack.append()
不返回值,因此默认返回值为
None
stack.append(…)
修改列表并返回
None
,从追加/删除的每一行中删除
stack=
,因为它不是必需的,并且正在将
None
分配给
stack