Problem
Code
public class MoveZeros {
public void solve(int[] numbers) {
int index = 0;
// 01. 0이 아닌 것을 array에 넣는다.
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] != 0) {
numbers[index++] = numbers[i];
}
}
// 02. index에서 numbers.length에 0을 넣는다.
while (index < numbers.length) {
numbers[index++] = 0;
}
System.out.println(Arrays.toString(numbers));
}
public static void main(String[] args) {
int[] numbers = new int[]{0, 3, 2, 0, 8, 5};
MoveZeros moveZeros = new MoveZeros();
moveZeros.solve(numbers);
}
}
Code Test Site
leetcode.com/problems/move-zeroes/