leetcode(4),Move Zeroes详解(python)

来源:互联网 发布:异构系统数据集成 编辑:程序博客网 时间:2024/06/05 00:43

question:

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.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

note:

1.不要复制数列,就在原数列上操作

2.尽可能少的操作


代码如下:

def movezeros(num):    count = len(num)    for i in range(count):        if num[i] == 0:            del num[i]            num.append(0)    print num


0 0
原创粉丝点击