Asked 5 months ago
0Comments
2 Views
sort(arr.begin() , arr.end())
void sortColors(vector<int>& nums) {
int ones , zeros , twos ;
zeros = ones = twos = 0 ;
for(int i = 0 ; i <nums.size() ; i++){
if(nums[i] == 0){
zeros++ ;
}
else if(nums[i] == 1 ){
ones++ ;
}else{
twos++ ;
}
}
int i = 0 ;
while(zeros--){
nums[i] = 0 ;
i++ ;
}
while(ones--){
nums[i] = 1 ;
i++ ;
}
while(twos--){
nums[i] = 2 ;
i++ ;
}
}
void sortColors(vector<int>& nums) {
int low = 0, medium = 0, high = nums.size() - 1;
while(medium <= high) {
if(nums[medium] == 0) {
swap(nums[low], nums[medium]);
low++;
medium++;
} else if(nums[medium] == 1) {
medium++;
} else {
swap(nums[medium], nums[high]);
high--;
}
}
}
Share