Skip to content

Commit

Permalink
Time: 0 ms (100.00%), Space: 41.6 MB (77.44%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
kjy0349 committed Jun 13, 2024
1 parent 8055498 commit 10b6eb6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 0075-sort-colors/0075-sort-colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
private void swap(int[] array, int left, int right) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
}

private void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return ;
}
int pivot = nums[left];
int i = left + 1;
int j = right;
while (i <= j) {
while (i <= j && nums[i] < pivot) {
i++;
}
while (i <= j && nums[j] > pivot) {
j--;
}
if (i <= j) {
swap(nums, i, j);
i++; j--;
}
}
swap(nums, left, j);
quickSort(nums, left, j - 1);
quickSort(nums, j + 1, right);
}

public void sortColors(int[] nums) {
quickSort(nums, 0, nums.length - 1);
}
}

0 comments on commit 10b6eb6

Please sign in to comment.