大家好,我正在leetcode上练习Python编码问题:
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
这是我的代码:
class Solution:
def removeDuplicates(self,nums):
result = []
result.append(nums[0])
for n in range(len(nums)):
result_ref = False
for x in range(n):
if nums[x] == nums[n]:
result_ref = False
else:
result_ref = True
if result_ref:
result.append(nums[n])
else:
continue
return result
该功能在我的计算机上可以正常工作,但是我一直在我的leetcode控制台中收到此错误。有人知道如何解决吗?非常感谢你!
TypeError: slice indices must be integers or None or have an __index__ method
Line 25 in integerListToString (Solution.py)
Line 42 in main (Solution.py)
Line 48 in <module> (Solution.py)
答案 0 :(得分:1)
请查看默认代码@ https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
rtype应该为int,但您返回布尔值
将“结果”更改为int时,该结果将消失:
TypeError: slice indices must be integers or None or have an __index__ method
btw afaik Python 2太旧了-imo Python 3.x更加有趣;)