Skip to content

Commit

Permalink
Sort_Colors #LeetCode75 in JAVA
Browse files Browse the repository at this point in the history
Solution for Leetcode problem no.75 [SORT-COLORS]
  • Loading branch information
naman648 authored and x0lg0n committed Oct 31, 2024
1 parent 07a4e40 commit 0744fb0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Java/SortColors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class SortColors {
public void sortColors(int[] nums) {
int n = nums.length;
int low = 0;
int mid = 0;
int high = n-1;
while(mid<=high){
if(nums[mid]==0){
swap(nums,low,mid);
low++;
mid++;
}
else if(nums[mid]==1){
mid++;
}
else{
swap(nums,mid,high);
high--;
}
}
}
private void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

0 comments on commit 0744fb0

Please sign in to comment.