2017.9.18
数组划分,采用的就是快排的思路。
细节需要处理一下,比如数组都小于k的时候,要返回数组的长度。
以及当nums【height】< k时,位置需要向后加1.
public class Solution {
/*
* @param nums: The integer array you should partition
* @param k: An integer
* @return: The index after partition
*/
public static int partitionArray(int[] nums, int k) {
// write your code here
if(nums == null ||nums.length == 0){
return 0;
}
int low = 0;
int height = nums.length-1;
int tmp = nums[height];
while(low < height){
while(low < height && nums[low] < k){
low ++;
}
nums[height] = nums[low];
while(low < height && nums[height] >= k){
height --;
}
nums[low] = nums[height];
}
if(tmp < k){
return low+1;
}
if(nums[nums.length-1] < k){
return nums.length;
}
return low;
}
}