#99. 数组零元素前移
数组零元素前移
Move Zeros to Start
Background
Congcong is organizing some numbers and wants to group all zeros at the beginning to make them look tidier.
Problem Description
Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of the non-zero numbers does not matter. For example, {1, 0, 0, 1} becomes {0, 0, 1, 1}. You may modify and return the given array or make a new array.
Input Format
The input consists of a single line string, representing the original array in the format [num1 num2 ... numN], where elements are separated by spaces.
[num1 num2 ... numN]
Output Format
Output the rearranged array in the format [x, y, z, ...], with elements separated by commas and spaces.
[element1, element2, ..., elementN]
Sample
[1 0 0 1]
[0, 0, 1, 1]
[0 1 1 0 1]
[0, 0, 1, 1, 1]
[1 0]
[0, 1]
Sample Explanation
In the first sample, the original array is [1 0 0 1]. After moving all zeros to the start of the array, it becomes [0, 0, 1, 1].
Constraints
The length of the array satisfies . The value of each element in the array satisfies .