Leetcode 283.移动零

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
# for i in range(len(nums))[::-1]:
# if nums[i]==0:
# nums.pop(i)
# nums.append(0)
for i in range(len(nums)-1,-1,-1):
if nums[i]==0:
nums.pop(i)
nums.append(0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
count=0
for i in range(len(nums)):
if nums[i]!=0:
nums[count]=nums[i]
count+=1
while count<len(nums):
nums[count]=0
count+=1