#31 Partition Array

原创 2016年08月29日 11:21:23

题目描述:

Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:

  • All elements < k are moved to the left
  • All elements >= k are moved to the right

Return the partitioning index, i.e the first index i nums[i] >= k.

 Notice

You should do really partition in array nums instead of just counting the numbers of integers smaller than k.

If all elements in nums are smaller than k, then return nums.length

Example

If nums = [3,2,2,1] and k=2, a valid answer is 1.

Challenge 

Can you partition the array in-place and in O(n)?

题目思路:

这题也可以用two pointers的想法,l和r指针一个在头,一个在尾,i从0....r遍历,如果nums[i] < k,就l和i上的值交换;反之,则让r和i上的值交换。这里要注意的是,r换给i的值可能是个< k的东西,这种情况下,需要再和l换一次。

Mycode(AC = 13ms):

class Solution {
public:
    int partitionArray(vector<int> &nums, int k) {
        // write your code here
        if (nums.size() == 0) return 0;
        
        int l = 0, r = nums.size() - 1;
        for (int i = 0; i <= r; i++) {
            // swap left and index if nums[i] < k
            if (nums[i] < k) {
                swap(nums, i, l);
                l++;
            }
            // swap right and index if nums[i] < k
            else {
                swap(nums, i, r);
                // it is possible that the original
                // nums[r] is smaller than k, in that
                // case, should do another swap between
                // l and old i
                if (nums[i] < k) i--;
                r--;
            }
        }
        
        return l;
    }
    
    void swap(vector<int> &nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
};


版权声明:三千微尘里,吾宁爱与憎

Lintcode31 Partition Array solution题解

【题目描述】 Given an array nums of integers and an int k, partition the array (i.e move the elements...

LeetCode-561 Array Partition I

LeetCode 561 Array Partition I,对数组进行排序后求偶数位和的新颖思路
  • NOadu
  • NOadu
  • 2017年06月02日 15:50
  • 262

[LeetCode]561. Array Partition I (数组分区 1)

561. Array Partition IGiven an array of 2n integers, your task is to group these integers into n pai...

561. Array Partition I/581. Shortest Unsorted Continuous Subarray

Array Partition I description implementation561. Array Partition IdescriptionGiven an array of 2n in...

Leetcode 561.Array Partition I

561.Array Partition IDescription
  • peisir
  • peisir
  • 2017年07月06日 23:19
  • 100

leetcode题解-561. Array Partition I && 62. Unique Paths && 63. Unique Paths II

561,题目:Given an array of 2n integers, your task is to group these integers into n pairs of integer, ...

微软夏令营笔试测验第一题 Array Partition

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer array A1, A2 … AN, you are asked to spli...

31. Next Permutation \ 33. Search in Rotated Sorted Array

next permutation 算法思想 实现 Search in Rotated Sorted Array31. next permutation算法思想对当前排列从后向前扫描,找到一对为升序的相...

partition magic

  • 2013年11月21日 00:55
  • 12.23MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:#31 Partition Array
举报原因:
原因补充:

(最多只允许输入30个字)