编写一个算法,该算法采用一个数组并将所有零移动到末尾,同时保留其他元素的顺序。
move_zeros([1, 0, 1, 2, 0, 1, 3]) # returns [1, 1, 2, 1, 3, 0, 0]
题目难度:简单
题目来源:https://www.codewars.com/kata/52597aa56021e91c93000cb0
def move_zeros(lst: list) -> list:
return lst
assert move_zeros([1, 2, 0, 1, 0, 1, 0, 3, 0, 1]) == [1, 2, 1, 1, 3, 1, 0, 0, 0, 0]
assert move_zeros([9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]) == [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert move_zeros([0, 0]) == [0, 0]
assert move_zeros([0]) == [0]
assert move_zeros([]) == []