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.
Have you ever encountered this problem in a real interview? YesSample Example
give the array nums= [3,2,2,1] and k=2, return 1
Note
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.
challenges
Requires that the time complexity of O (n) be used in situ to divide the array
label Expand
Related TopicsExpand
Analysis: The principle is the partition of the fast line
Code:
Class Solution {public: int Partitionarray (vector<int> &nums, int. k) { //write your code here int P = 0; for (int i=0;i<nums.size (); i++) { if (nums[i]<k) { swap (nums[i],nums[p]); p++; } } return p; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
lintcode-Array Partitioning