Created
December 25, 2020 18:36
-
-
Save Ram-1234/f5728edc89e4650c9e311afa9ec64bff to your computer and use it in GitHub Desktop.
Sort Colors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JAVA CODE | |
class Solution { | |
public void sortColors(int[] nums) { | |
int zero=0,one=0,two=0; | |
for(int i:nums){ | |
if(i==0) zero++; | |
if(i==1) one++; | |
if(i==2) two++; | |
} | |
int i=0; | |
while(zero-->0) | |
nums[i++]=0; | |
while(one-->0) | |
nums[i++]=1; | |
while(two-->0) | |
nums[i++]=2; | |
} | |
} | |
////// | |
INPUT | |
Input: nums = [2,0,2,1,1,0] | |
///// | |
OUTPUT | |
Output: [0,0,1,1,2,2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment