我正在嘗試在 Leetcode 上練習我的 Python。我在嘗試將其作為整數串列回傳時遇到型別轉換問題。從一些研究來看,這似乎是因為我在 for 回圈中使用了“范圍”。有人可以告訴我正確的方法來做到這一點而不會導致錯誤嗎?這是我的代碼:
class Solution(object):
def removeElement(self, nums, val):
for i in range(nums.count(val)):
nums.remove(val)
return nums
這是錯誤訊息:
TypeError: [2, 2] is not valid value for the expected return type integer[]
raise TypeError(str(ret) " is not valid value for the expected return type
integer[]");
Line 39 in _driver (Solution.py)
_driver()
第 45 行(Solution.py)
這樣做我也遇到同樣的錯誤
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
while(nums.count(val) != 0):
nums.remove(val)
return nums
uj5u.com熱心網友回復:
我想到了
while 環形:
while(nums.count(val) != 0):
nums.remove(val)
return len(nums)
for 環形:
for i in range(nums.count(val)):
nums.remove(val)
return len(nums)
Mark 指出回傳型別應該是 an int,所以我將 thearray作為 an參考int,它可以作業。謝謝馬克!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335423.html
上一篇:在回圈串列中查找缺失的序列
下一篇:總和等于一個數字的子串
