Problem: Partition Array (LintCode)
思路
复杂度
public class Solution {
/**
*@param nums: The integer array you should partition
*@param k: As description
*return: The index after partition
*/
public int partitionArray(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return 0;
}
int i = 0;
int j = nums.length - 1;
for (; i <= j; i++) {
if (nums[i] < k) {
continue;
}
while (i <= j && nums[j] >= k) {
j--;
}
if (i <= j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
j--;
}
}
return j + 1;
}
}易错点
follow up
Last updated