#99. 数组零元素前移

    ID: 99 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatArray-2gesp3一维数组

数组零元素前移

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 NN satisfies 1N1051 \le N \le 10^5. The value of each element valval in the array satisfies 109val109-10^9 \le val \le 10^9.