java sortcolor

• ``````class Solution {
public void sortColors(int[] nums) {
int zero = -1;          // [0...zero] == 0 //初始化的时候，肯定不在该闭区间内
int two = nums.length;  // [two...n-1] == 2 //初始化的时候，肯定不在该闭区间内
for( int i = 0 ; i < two ; ){
if( nums[i] == 1 ) {
i++;
} else if ( nums[i] == 2 ) {
swap(nums, i, --two);
} else{ // nums[i] == 0
assert( nums[i] == 0 );
swap(nums, ++zero , i++ );
}
}
}
private void swap(int[] nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
``````

• ``````class Solution {
public void sortColors(int[] nums) {
int zero = -1;          // [0...zero] == 0 //初始化的时候，肯定不在该闭区间内
int two = nums.length;  // [two...n-1] == 2 //初始化的时候，肯定不在该闭区间内
for( int i = 0 ; i < two ; ){
if( nums[i] == 1 ) {
i++;
} else if ( nums[i] == 2 ) {
swap(nums, i, --two);
} else{ // nums[i] == 0
assert( nums[i] == 0 );
swap(nums, ++zero , i++ );
}
}
}
private void swap(int[] nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.