DEV Community

Karleb
Karleb

Posted on

#75. Sort Colors

https://leetcode.com/problems/sort-colors/description/?envType=daily-question&envId=2024-06-13


var sortColors = function(nums) {
  let low = 0, mid = 0, high = nums.length - 1

  while (mid <= high) {
    if (nums[mid] === 0) {
        [nums[low], nums[mid]] = [nums[mid], nums[low]]
        low++
        mid++
    } else if (nums[mid] === 1) {
        mid++
    } else {
         [nums[mid], nums[high]] = [nums[high], nums[mid]]
         high--
    }
  }
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay