티스토리 뷰

Problem Solving

[leetcode][C++] sort colors

틔틔 2021. 10. 15. 22:44

https://leetcode.com/problems/sort-colors/

 

Sort Colors - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

[leetcode][C++] sort colors

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int low = 0;
        int high = nums.size() - 1;

        for (int i = 0; i <= high;) {
            if (nums[i] == 0) {
                swap(nums[i], nums[low]);
                low++; i++;
            }
            else if (nums[i] == 2) {
                swap(nums[i], nums[high]);
                high--;
            }
            else i++;
        }
    }
};
댓글