Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Sort Colors problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Sort Colors problem solution we have 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 0, 1, and 2 to represent the color red, white, and blue, respectively.

leetcode sort colors problem solution

Leetcode Sort Colors problem solution in Python.

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        curr = 0
        for _ in range(len(nums)):
            if nums[curr] == 0:
                nums.pop(curr)
                nums.insert(0, 0)
                curr += 1
            elif nums[curr] == 2:
                nums.pop(curr)
                nums.append(2)
            else:
                curr += 1
        return

Sort Colors problem solution in Java.

public void sortColors(int[] nums) {
        int len = nums.length;
        int zero=0,one=0,two=0;
        for(int i=0;i<len;i++){
            if(nums[i] == 0){
                zero++;
            }else if(nums[i]==1){
                one++;
            }else{
                two++;
            }
        }
        for(int i=0;i<zero;i++){
            nums[i] = 0;
        }
        for(int i=zero;i<zero+one;i++){
            nums[i] = 1;
        }
        for(int i=zero+one;i<len;i++){
            nums[i] = 2;
        }
}

Problem solution in C++.

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int top = 0; 
        int bot = nums.size()-1;
        int ct1 = 0;
        int ct2 = 0;
        while (top<=bot) {
            if (nums[top] == 0) ct1++;
            if (nums[top] == 1) ct2++;            
            top++;
        }
        for (int i = 0; i <= bot; i++) {
            if (ct1-->0) nums[i] = 0;
            else if(ct2-->0) nums[i] = 1;
            else nums[i] = 2;
        }
        
    }
};

Problem solution in C.

void swap(int *a, int *b)
{    
    *a = *a^*b;
    *b = *a^*b;
    *a = *a^*b;
}

void sortColors(int* nums, int numsSize){
    
    int zeros = 0, ones = 0, twos = numsSize - 1;
    
    while (ones <= twos) {
        if (nums[ones] == 0) {
            if (ones != zeros)
                swap(&nums[ones], &nums[zeros]);
            ones++;
            zeros++;
        } else if (nums[ones] == 1) {
            ones++;
        } else {
            if (twos != ones)
                swap(&nums[twos], &nums[ones]);
            twos--;
        }
    }

}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

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

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes