Sort Colors Leetcode Solution

In this post, you will know how to solve the Sort Colors Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

Sort Colors Leetcode Solution
Sort Colors Leetcode Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Leetcode by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

We will use the integers 01, and 2 to represent the color red, white, and blue, respectively.

You must solve this problem without using the library’s sort function.

Example 1:

Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Example 2:

Input: nums = [2,0,1]
Output: [0,1,2]

Constraints:

  • n == nums.length
  • 1 <= n <= 300
  • nums[i] is either 01, or 2.

Follow up: Could you come up with a onepass algorithm using only constant extra space?

Sort Colors Leetcode Solutions in Python

class Solution(object):
    def sortColors(self, nums):
        n=len(nums)
        zero,one,two=0,0,n-1
        while one<=two:
            if nums[one]==1:
                one+=1
            elif nums[one]==2:
                nums[one],nums[two]=nums[two],nums[one]
                two-=1
            else:
                nums[zero],nums[one]=nums[one],nums[zero]
                zero+=1
                one+=1

Sort Colors Leetcode Solutions in CPP

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int n = nums.size();
        int i = 0,j = 0,k = n-1;
        
        while(i<=k)
        {
            if(nums[i] == 0)
            {
                swap(nums[i++],nums[j++]);
            }
            else if(nums[i] == 1)
            {
                i++;
            }
            else if(nums[i] == 2)
            {
                swap(nums[i],nums[k]);
                k--;
            }
        }
    }
};

Sort Colors Leetcode Solutions in Java

class Solution {
    public void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    public void sortColors(int[] nums) {
        int i=0,j=0,k=nums.length-1;
        while(i <= k){
            if(nums[i] == 0){
                swap(nums,i,j);
                i++;
                j++;
            }else if(nums[i] == 1){
                i++;
            }else if(nums[i] == 2){
                swap(nums,i,k);
                k--;
            }
        }
    }
}

Note: This problem Sort Colors is generated by Leetcode but the solution is provided by  BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Next: Search a 2D Matrix Leetcode Solution

Leave a Reply

Your email address will not be published. Required fields are marked *