Make Unreal REAL.
article thumbnail
283. Move Zeroes

 

 

빠른 투 포인터 문제다.

  • l, r 둘 중 하나의 이동 속도가 다른 하나보다 빠른 투 포인터다.
  • 보통 r을 순차적으로 증가시키고, l은 조건에 따라 당겨오는 방식이다.
  • 조건을 만족하는 최소 구간을 찾을 때 유용하다.

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution
{
public:
    void moveZeroes(vector<int>& nums)
    {
        for (int z = 0, i = 0; i < nums.size(); ++i)
            if (nums[i] != 0)
                swap(nums[z++], nums[i]);
    }
};
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그