You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.# Python 1classSolution:
defmoveZeroes(self, nums: List[int]) ->None:
""" type nums: List[int] Do not return anything, modify nums in-place instead. """count=nums.count(0)
nums[:] = [iforiinnumsifi!=0]
nums+= [0] *count# Python 2classSolution:
defmoveZeroes(self, nums: List[int]) ->None:
""" type nums: List[int] Do not return anything, modify nums in-place instead. """j=0foriinrange(len(nums)):
ifnums[i] !=0:
nums[i], nums[j] =nums[j], nums[i]
j+=1
nums[:] 属于浅拷贝。列表内的元素内存地址并未发生改变。
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: