💬 Info
- 난이도 : Easy
- 문제 링크 : https://leetcode.com/problems/move-zeroes
- 풀이 링크 : LeetCode/Easy/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.
Note that you must do this in-place without making a copy of the array.
풀이 시간 : 5분
class Solution {
public void moveZeroes(int[] nums) {
int index = 0;
for (int num : nums) {
if (num != 0) {
nums[index++] = num;
}
}
while (index < nums.length) {
nums[index++] = 0;
}
}
}