File tree 2 files changed +22
-0
lines changed
2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change 69
69
| 71 | [ Simplify Path] ( https://leetcode.com/problems/simplify-path ) | [ ![ Java] ( assets/java.png )] ( src/SimplifyPath.java ) | |
70
70
| 73 | [ Set Matrix Zeroes] ( https://leetcode.com/problems/set-matrix-zeroes ) | [ ![ Java] ( assets/java.png )] ( src/SetMatrixZeroes.java ) | |
71
71
| 74 | [ Search a 2D Matrix] ( https://leetcode.com/problems/search-a-2d-matrix ) | [ ![ Java] ( assets/java.png )] ( src/SearchA2DMatrix.java ) | |
72
+ | 75 | [ Sort Colors] ( https://leetcode.com/problems/sort-colors ) | [ ![ Java] ( assets/java.png )] ( src/SortColors.java ) | |
72
73
| 83 | [ Remove Duplicates from Sorted List] ( https://leetcode.com/problems/remove-duplicates-from-sorted-list ) | [ ![ Java] ( assets/java.png )] ( src/RemoveDuplicatesFromSortedList.java ) [ ![ Python] ( assets/python.png )] ( python/remove_duplicates_from_linked_list.py ) | |
73
74
| 88 | [ Merge Sorted Array] ( https://leetcode.com/problems/merge-sorted-array ) | [ ![ Java] ( assets/java.png )] ( src/MergeSortedArray.java ) [ ![ Python] ( assets/python.png )] ( python/merge_sorted_array.py ) | |
74
75
| 94 | [ Binary Tree Inorder Traversal] ( https://leetcode.com/problems/binary-tree-inorder-traversal/ ) | [ ![ Java] ( assets/java.png )] ( src/BinaryTreeInorderTraversal.java ) [ ![ Python] ( assets/python.png )] ( python/binary_tree_inorder_traversal.py ) | |
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/sort-colors
2
+ // T: O(n) single pass solution
3
+ // S: O(1)
4
+
5
+ public class SortColors {
6
+ public void sortColors (int [] nums ) {
7
+ int zerosIndex = 0 ;
8
+ int onesIndex = 0 ;
9
+
10
+ for (int i = 0 ; i < nums .length ; i ++) {
11
+ int temp = nums [i ];
12
+ nums [i ] = 2 ;
13
+ if (temp == 0 ) {
14
+ nums [onesIndex ++] = 1 ;
15
+ nums [zerosIndex ++] = 0 ;
16
+ } else if (temp == 1 ) {
17
+ nums [onesIndex ++] = 1 ;
18
+ }
19
+ }
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments