В чем разница между следующими двумя ответами на leetcode 27. Удалить элемент ? Оба они возвращают один и тот же результат, но только номер 1 был принят. Я понятия не имею, почему № 2 не является приемлемым.
Вывод
#Output
[2,2]
Программы
№ 1
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
for x in nums[:]:
if x == val:
nums.remove(x)
return nums
№ 2
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
result = [s for s in nums if s != val]
result = str(result).replace(' ', '')
return result