Title Description:
Gives an array of integers nums and an integer k. Dividing an array (that is, moving an element in an array nums) makes:
-
- All elements less than k are moved to the left
- All elements greater than or equal to K are moved to the right
Returns the position of the array, that is, the first position in the array I, satisfies nums[i] is greater than or equal to K.
Precautions
You should really divide the array nums, not just the number of integers smaller than k, if all the elements in the array nums are smaller than K, the Nums.length is returned.
Sample Example
Give the array nums=[3,2,2,1] and k=2, return 1
1 Public classSolution {2 /** 3 *@paramnums:the integer array you should partition4 *@paramK:as Description5 *return:the Index after partition6 */7 Public intPartitionarray (int[] Nums,intk) {8 intLength =nums.length;9 for(inti=0;i<length;i++){Ten if(nums[i]>=k) { One for(intj=length-1;j>=i;j--){ A if(nums[j]<k) { - inttemp; -temp =Nums[i]; theNums[i] =Nums[j]; -NUMS[J] =temp; -Length = j+1; - Break; + } - if(j==i) { + returnJ; A } at } - } - } - returnnums.length; - } -}
lintcode-Array Partitioning