Home leetcode 풀이 - move zeros
Post
Cancel

leetcode 풀이 - move zeros

문제

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.

Example 1:

Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] Example 2:

Input: nums = [0] Output: [0]

Constraints:

1 <= nums.length <= 104 -231 <= nums[i] <= 231 - 1

Follow up: Could you minimize the total number of operations done?


내 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
    public void moveZeroes(int[] nums) {
        // 포인터 두 개를 사용해 배열을 한번만 돌도록 한다. 
        //sol: using two pointer
        int first =0;
        int second =1; 

        // saved value for switching index
        // 자리 바꿀 때 임시로 값 저장할 변수
        int saved =0;
        
        while(second < nums.length){
            // 0 이 0 아닌 숫자 앞에 있는 경우만 자리 바꾼다.
            if(nums[first]==0 && nums[second]!=0){  // if zero is in front of non-zero value, switch
                saved = nums[first];
                nums[first]=nums[second];
                nums[second] = saved;
                ++first;
                ++second;
            // 0 아닌 수가 앞에 있다면 인덱스 증가시킴 (자리 바꿀 필요 없음)
            }else if(nums[first]!=0){  // if non zero is in front of zero, you don't have to switch. Just move two pointers forward. 
                ++first;
                ++second;
            // 0 끼리 비교 -> 첫번째 0 자리 기억하기 위해 두번째 포인터만 증가시킴 
            }else{  // if you compare two zero numbers, move second pointer forward to remember first zero number's index. 
                ++second;
            }
        }
           
    }
}
This post is licensed under CC BY 4.0 by the author.