LeetCode 풀기 - 283. Move Zeros

백지우·2026년 10월 1일

LeetCode

목록 보기
75/76
  • LeetCode 최소 하루에 하나는 풀자는 마인드
  • 사용언어: JAVA

283. Move Zeros

풀이

  • 방법: index를 이용해 전체 탐색, cur는 0이 아닌 요소가 채워져야 하는 index를 나타냄. 0이 아닌 수가 나타나면 swap
class Solution {
    public void moveZeroes(int[] nums) {

        int index = 0;
        int cur = 0;

        while(index < nums.length){
            if(nums[index] != 0){
                swap(nums, cur, index);
                cur++;
            }

            index++;
        } 


    }
    private void swap(int[] nums, int a, int b){
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;

    }
}
  • 코드 작성하다가 느낀점
    - 쉬운거라 그런지 뭔가 감이 잡힌 느낌

0개의 댓글