diff --git a/C++/Sort_Colors_leetcode.cpp b/C++/Sort_Colors_leetcode.cpp new file mode 100644 index 0000000..1cf784a --- /dev/null +++ b/C++/Sort_Colors_leetcode.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + void sortColors(vector& nums) { + + int low=0; + int mid=0; + int high =nums.size()-1; + + while(mid<=high){ + + switch(nums[mid]){ + case 0: + swap(nums[low++], nums[mid++]); + break; + + //if mid equal to 1 + case 1: + mid++; + break; + + case 2: + swap(nums[mid], nums[high--]); + break; + } + } + } +};