[JAVA] 283. Move Zeroes 풀이
리트코드 283. Move Zeroes 풀이
📖 JAVA
Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
정수 배열이 주어진다, 0이 아닌 요소들의 순서를 해치지 않으면서 배열안의 0들을 배열의 뒷쪽으로 밀어라.
Note that you must do this in-place without making a copy of the array.
배열을 다른곳에 복사하지 않고 코드를 짜야함.
Could you minimize the total number of operations done?
전체 연산 숫자를 적게할 수는 없을까?
📖 풀이
public int[] moveZeroes(int[] nums) {
int index=0;
for (int num:nums) {
if(num != 0){
nums[index++] = num;
}
}
for (int i = index; i < nums.length; i++){
nums[i] = 0;
}
return nums;
}