LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  void sortColors(vector<int>& nums) {
    int cur = 0;
    int l = 0;
    int r = size(nums);
    while (cur < r) {
      if (nums[cur] == 0 && nums[l] == 0) {
        ++cur;
        ++l;
      } else if (nums[cur] == 0) {
        swap(nums[cur], nums[l++]);
      } else if (nums[cur] == 2) {
        swap(nums[cur], nums[--r]);
      } else {
        ++cur;
      }
    }
  }
};
class Solution {
 public:
  void sortColors(vector<int>& nums) {
    int cur = 0;
    int l = 0;
    int r = size(nums);
    while (cur < r) {
      if (nums[cur] == 0) {
        swap(nums[cur++], nums[l++]);
      } else if (nums[cur] == 2) {
        swap(nums[cur], nums[--r]);
      } else {
        ++cur;
      }
    }
  }
};