My java o(n) solution


  • -1
    C
     public class Solution {
       public void sortColors(int[] nums) {
        int p0=0;
        int p1=0;
        int p2=0;
        for(int i=0;i<nums.length;i++)
        {
            if(nums[i] == 0){
                p0++;
            }
            else if(nums[i] == 1){
                p1++;
            }
            else if(nums[i] == 2){
                p2++;
            }
        }
        
        for(int i=0;i<nums.length;i++)
        {
            if(i <p0){
                nums[i] = 0;
            }   
            else if( (p0<=i)&& (i<(p0+p1))){
                nums[i] = 1;
            }
            else if(((p0+p1)<= i)){
                nums[i] = 2;
            }
        }
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.