您的位置:首页 > 编程语言 > Python开发

lintcode刷题-移动零 python

2017-03-23 20:51 495 查看
给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
注意事项1.必须在原数组上操作2.最小化操作数样例给出 nums =
[0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].

class Solution:
# @param {int[]} nums an integer array
# @return nothing, do this in-place
def moveZeroes(self, nums):
# Write your code here

i=0
j=0
while i<len(nums):
if nums[i] != 0:
nums[j]=nums[i]
j+=1
i+=1
else:
i+=1
while j<len(nums):
nums[j]=0
j+=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows python