Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e5d6c58

Browse files
solves sort colors
1 parent a715532 commit e5d6c58

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
| 71 | [Simplify Path](https://leetcode.com/problems/simplify-path) | [![Java](assets/java.png)](src/SimplifyPath.java) | |
7070
| 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes) | [![Java](assets/java.png)](src/SetMatrixZeroes.java) | |
7171
| 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) | |
7273
| 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) | |
7374
| 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) | |
7475
| 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) | |

src/SortColors.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

0 commit comments

Comments
 (0)