Skip to content

Commit 3602baf

Browse files
committed
1031
1 parent 5bcfaff commit 3602baf

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,4 @@ Feel free to add issues, create pull requests and be a contributer.
215215
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
216216
| Leetcode | [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/description/) | [Java](./java/mergeInterval.java) | _O(nlogn)_ | _O(1)_ | Medium | |
217217
| Leetcode | [57. Insert Interval](https://leetcode.com/problems/insert-interval/description/) | [Java](./java/insertInterval.java) | _O(n)_ | _O(1)_ | Hard | |
218+
| Leetcode | [75. Sort Colors](https://leetcode.com/problems/sort-colors/description/) | [Java](./java/sortColors.java) | _O(n)_ | _O(1)_ | Medium | |

java/sortColors.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public void sortColors(int[] nums) {
3+
int blue = nums.length-1, red = 0;
4+
for(int i=0; i<=blue; i++)
5+
{
6+
while(nums[i]==2 && i<blue) swap(nums,i,blue--);
7+
while(nums[i]==0 && red<i) swap(nums,i,red++);
8+
}
9+
}
10+
11+
public void swap(int [] nums, int i, int j)
12+
{
13+
int temp = nums[i];
14+
nums[i] = nums[j];
15+
nums[j] = temp;
16+
}
17+
18+
}

0 commit comments

Comments
 (0)