Partition Array

public class Solution {
    /**
     * @param nums: The integer array you should partition
     * @param k: An integer
     * @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;
			while (i <= j) {
				while (i <= j && nums[i] < k) {
			i++;
		}
		while (i <= j && nums[j] >= k) {
			j--;
		}
		
		if (i <= j) {
			int t = nums[i];
			nums[i++] = nums[j];
			nums[j--] = t;
			}
		}
			return i; //while循环没有进行的情况下是i在j的右边,循环停止
	}
}

最后更新于

这有帮助吗?