Skip to content

Commit cf8ef48

Browse files
sort colors
1 parent 65fcec7 commit cf8ef48

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

‎EASY/src/easy/SortColors.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package easy;
2+
3+
public class SortColors {
4+
5+
6+
public void sortColors(int[] nums) {
7+
int zero = 0, two = nums.length-1;
8+
for(int i = 0; i <= two;){
9+
if(nums[i] == 0 && i > zero) swap(nums, i, zero++);
10+
else if(nums[i] == 2 && i < two) swap(nums, i, two--);
11+
else i++;
12+
}
13+
}
14+
15+
void swap(int[] nums, int m, int n){
16+
int temp = nums[m];
17+
nums[m] = nums[n];
18+
nums[n] = temp;
19+
}
20+
21+
22+
public static void main(String...args){
23+
// int[] nums = new int[]{0,1,2,0,2,1};
24+
// int[] nums = new int[]{0};
25+
// int[] nums = new int[]{2};
26+
int[] nums = new int[]{2,2,1};
27+
// int[] nums = new int[]{1,0};
28+
}
29+
}

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
|79|[Word Search](https://leetcode.com/problems/word-search/)|[Solution](../../blob/master/MEDIUM/src/medium/WordSearch.java)|O(m*n*l) ? |O(m*n)|Medium|Backtracking/DFS
5454
|78|[Subsets](https://leetcode.com/problems/subsets/)|[Solution](../../blob/master/MEDIUM/src/medium/Subsets.java)|O(n^2) ? |O(1)|Medium|Backtracking
5555
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)|[Solution](../../blob/master/HARD/src/hard/MinimumWindowSubstring.java)|O(n)|O(k)|Hard|Two Pointers
56-
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)|[Solution]|O(n)|O(1)|Medium
56+
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)|[Solution](../../blob/master/MEDIUM/src/medium/SortColors.java)|O(n)|O(1)|Medium| Two Pointers
5757
|68|[Text Justification](https://leetcode.com/problems/text-justification/)|[Solution](../../blob/master/HARD/src/hard/TextJustification.java)|O(n)|O(1)|Hard|
5858
|67|[Add Binary](https://leetcode.com/problems/add-binary/)|[Solution](../../blob/master/EASY/src/easy/AddBinary.java)|O(n)|O(1)|Easy|
5959
|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../../blob/master/EASY/src/easy/LengthofLastWord.java)|O(n)|O(1)|Easy|

0 commit comments

Comments
 (0)