Merge c5ea70b1ca9e1f4152fd8fa487d9d93c8b0ebd75 into b70121d377cb6005eb65f12b098cd5decd905669
This commit is contained in:
commit
ddb8609c19
@ -224,21 +224,32 @@ Output: [0,0,1,1,2,2]
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public void sortColors(int[] nums) {
|
public void sortColors(int[] nums) {
|
||||||
int zero = -1, one = 0, two = nums.length;
|
int p0=0;
|
||||||
while (one < two) {
|
int p1=0;
|
||||||
if (nums[one] == 0) {
|
for(int i=0;i<nums.length;i++)
|
||||||
swap(nums, ++zero, one++);
|
{
|
||||||
} else if (nums[one] == 2) {
|
if(nums[i]==1)
|
||||||
swap(nums, --two, one);
|
{
|
||||||
} else {
|
int temp=nums[i];
|
||||||
++one;
|
nums[i]=nums[p1];
|
||||||
|
nums[p1]=temp;
|
||||||
|
p1++;
|
||||||
|
}
|
||||||
|
else if(nums[i]==0)
|
||||||
|
{
|
||||||
|
int temp=nums[i];
|
||||||
|
nums[i]=nums[p0];
|
||||||
|
nums[p0]=temp;
|
||||||
|
if(p0<p1)
|
||||||
|
{
|
||||||
|
int k=nums[i];
|
||||||
|
nums[i]=nums[p1];
|
||||||
|
nums[p1]=k;
|
||||||
|
|
||||||
|
}
|
||||||
|
p0++;
|
||||||
|
p1++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void swap(int[] nums, int i, int j) {
|
|
||||||
int t = nums[i];
|
|
||||||
nums[i] = nums[j];
|
|
||||||
nums[j] = t;
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user