본문 바로가기
Beakjoon&프로그래머스/Java

[LeetCode/Java] Move Zeroes

by 현장 2025. 12. 31.

-Code

class Solution {
    public void moveZeroes(int[] nums) {
        int length = nums.length;
        int left = 0, right = 0;
        // 투포인터로 변경
        for (int i = 0; i < length; i++) {
            if (nums[right] == 0) {
                // 오른쪽이 0을 가리키면 ++
                right++;
            } else {
                // 0이 아니면 값을 바꾸고 left와 right + 1
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
                right++;
            }
        }
    }
}

 

'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글

[LeetCode/Java] Maximum Average Subarray I  (0) 2025.12.31
[LeetCode/Java] Is Subsequence  (0) 2025.12.31
[LeetCode/Java] Two Sum  (0) 2025.12.31
[LeetCode/Java] Valid Anagram  (0) 2025.12.31
[LeetCode/Java] Contains Duplicate  (0) 2025.12.31