Partition Array

思路:
用quicksort partition的思路,直接返回storedIndex就好了。代码如下

 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) {  
     // write your code here  
     if(nums == null || nums.length == 0)  
       return nums.length;  
     int res = 0;  
     for(int i = 0; i < nums.length;i++) {  
       if(nums[i] < k) {  
         int temp = nums[i];  
         nums[i] = nums[res];  
         nums[res] = temp;  
         res++;  
       }  
     }  
     return res;  
   }  
 }  

评论

此博客中的热门博文

776. Split BST

663. Equal Tree Partition

532. K-diff Pairs in an Array