Partition Array II
Given [4,3,4,1,2,3,1,2], and low = 2 and high = 3.
Change to [1,1,2,3,2,3,4,4].
([1,1,2,2,3,3,4,4] is also a correct answer, but [1,2,1,2,3,3,4,4] is not)Given [4,3,4,1,2,3,1,2], and low = 2 and high = 3.
Change to [1,1,2,3,2,3,4,4].
([1,1,2,2,3,3,4,4] is also a correct answer, but [1,2,1,2,3,3,4,4] is not) public class Solution {
/**
* @param nums an integer array
* @param low an integer
* @param high an integer
* @return nothing
*/
public void partition2(int[] nums, int low, int high) {
int left = 0, right = nums.length - 1, i = 0;
while (i <= right && left <= right) {
if (nums[i] < low) {
// 写这里的时候,要想到此时换过来的元素只有两种情况:
// 1. 此时 i==left,交换实际并无操作,之后left++,i++;
// 2. 此时 i>left,交换之后,i位置的数字之前其实已经被处理过(==low or high),
// 此时可以放心直接令left++,i++;
// 所以两种情况下的代码是一样的。
swap(nums, left++, i++);
} else if (nums[i] > high) {
swap(nums, right--, i);
} else {
i++;
}
}
}
private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}