Data Structure & Algorithm: Level-1 Problem #27. Moving Zeroes

Given a list of integers, move all zeroes to the end of the list while maintaining the relative order of the non-zero numbers. Note that you must do this in-place without creating a copy of the original list.

Sample Input:

[ 5, 6, 0, 0, 1, 0, 3, 4]

Sample Output:

[5, 6, 1, 3, 4, 0, 0, 0]


数据结构和算法:初级练习题 #27 – 移动数组里的零


给定一个数组,把数组里的零都移到末尾,其他数字的相对位置保持不变。注意我们需要在当前数组里操作,而不能拷贝数组。

输入样例:

[ 5, 6, 0, 0, 1, 0, 3, 4]

输出样例:

[5, 6, 1, 3, 4, 0, 0, 0]


Python Solution

Moving Zeroes