#106. 数组重排:3后跟4
数组重排:3后跟4
Three Followed by Four
Background
Dawei is organizing a pile of numbers. He has a special preference: he wants every number to be immediately followed by a number .
Problem Description
Given an array of integers, rearrange the elements such that every number is immediately followed by a number . Note:
- The positions of numbers cannot be moved.
- All other numbers may be moved.
- The array is guaranteed to contain the same number of s and s.
- It is guaranteed that every is followed by a number that is not a .
- It is guaranteed that a appears in the array before any .
Input Format
Input is given from standard input in the following format.
A single line containing several integers, separated by spaces, representing the elements of the original array.
Output Format
Output is printed to standard output in the following format.
A single line containing the rearranged array elements, separated by commas and enclosed in square brackets
[].
Sample
1 3 1 4
[1, 3, 4, 1]
1 3 1 4 4 3 1
[1, 3, 4, 1, 1, 3, 4]
3 2 2 4
[3, 4, 2, 2]
Sample Explanation
For Sample 1: The original array is [1, 3, 1, 4]. The number is at index . We need to place a after it. The original is at index . We can move the to index , resulting in [1, 3, 4, 1].
Constraints
The time limit for each test case is second, and the memory limit is KiB.