Update Leetcode 题解 - 排序.md
heihei
This commit is contained in:
parent
2c31eddf34
commit
c5ea70b1ca
@ -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;
|
||||||
|
|
||||||
private void swap(int[] nums, int i, int j) {
|
}
|
||||||
int t = nums[i];
|
p0++;
|
||||||
nums[i] = nums[j];
|
p1++;
|
||||||
nums[j] = t;
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user