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)?

此题是利用快速排序的思想,比较简单吧算是,但是细节需要注意一下。

有个错误解法,如下:

  1. public class Solution {
  2. /**
  3. *@param nums: The integer array you should partition
  4. *@param k: As description
  5. *return: The index after partition
  6. */
  7. public int partitionArray(int[] nums, int k) {
  8. int left = 0;
  9. int right = nums.length - 1;
  10. while (left < right) {
  11. while (left < right && nums[left] < k) {
  12. left++;
  13. }
  14. while (left < right && nums[right] >= k){
  15. right--;
  16. }
  17. if (left >= right) {
  18. break;
  19. }
  20. swap(nums, left, right);
  21. left++;
  22. right--;
  23. }
  24. return left;
  25. }
  26. public void swap(int[] nums, int a, int b) {
  27. int tmp = nums[a];
  28. nums[a] = nums[b];
  29. nums[b] = tmp;
  30. }
  31. }

这种解法没有考虑到全部数字都小于target的情况,while里面的条件应该是left<=right,这样,即使循环到最后一步,left和right重合,left还要++才能返回数组的长度。

正确解法如下:

  1. public class Solution {
  2. /**
  3. *@param nums: The integer array you should partition
  4. *@param k: As description
  5. *return: The index after partition
  6. */
  7. public int partitionArray(int[] nums, int k) {
  8. int left = 0;
  9. int right = nums.length - 1;
  10. while (left <= right) {
  11. while (left <= right && nums[left] < k) {
  12. left++;
  13. }
  14. while (left <= right && nums[right] >= k){
  15. right--;
  16. }
  17. if (left > right) {
  18. break;
  19. }
  20. swap(nums, left, right);
  21. left++;
  22. right--;
  23. }
  24. return left;
  25. }
  26. public void swap(int[] nums, int a, int b) {
  27. int tmp = nums[a];
  28. nums[a] = nums[b];
  29. nums[b] = tmp;
  30. }
  31. }

当然我当时的第一想法不是这样做的,while条件里面只要不越界就可以继续循环:

  1. public class Solution {
  2. /**
  3. *@param nums: The integer array you should partition
  4. *@param k: As description
  5. *return: The index after partition
  6. */
  7. public int partitionArray(int[] nums, int k) {
  8. int left = 0;
  9. int right = nums.length - 1;
  10. while (left < right) {
  11. while (left < nums.length && nums[left] < k) {
  12. left++;
  13. }
  14. while (right >= 0 && nums[right] >= k){
  15. right--;
  16. }
  17. if (left >= right) {
  18. break;
  19. }
  20. swap(nums, left, right);
  21. left++;
  22. right--;
  23. }
  24. return left;
  25. }
  26. public void swap(int[] nums, int a, int b) {
  27. int tmp = nums[a];
  28. nums[a] = nums[b];
  29. nums[b] = tmp;
  30. }
  31. }

也通过了。。。有一点需要注意的是,不要把left++写到while里面,通不过,也是奇怪得很。。。

  1. while (left < nums.length && nums[left++] < k);

这样。。。

Partition Array的更多相关文章

  1. LintCode 373: Partition Array

    LintCode 373: Partition Array 题目描述 分割一个整数数组,使得奇数在前偶数在后. 样例 给定[1, 2, 3, 4],返回[1, 3, 2, 4]. Thu Feb 23 ...

  2. Lintcode: Partition Array

    Given an array "nums" of integers and an int "k", Partition the array (i.e move ...

  3. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...

  4. Lintcode373 Partition Array by Odd and Even solution 题解

    [题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...

  5. [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  6. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  7. LeetCode 1013 Partition Array Into Three Parts With Equal Sum 解题报告

    题目要求 Given an array A of integers, return true if and only if we can partition the array into three  ...

  8. Partition Array into Disjoint Intervals LT915

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  9. Partition Array Into Three Parts With Equal Sum LT1013

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

随机推荐

  1. java中List集合及其遍历详解

    1. 首先List<E>集合继承与Collection<E>,是一个接口. ①  Collection (集合框架是JDK1.2版本出现的) ②   list:是有序的,元素可 ...

  2. 最短路径算法之三——Bellman-Ford算法

    Bellman-Ford算法 Dijkstra算法无法判断含负权边的图的最短路. 如果遇到负权,在没有负权回路存在时,即便有负权的边,也可以采用Bellman-Ford算法正确求出最短路径. PS:负 ...

  3. python脚本工具-1 制作爬虫下载网页图片

    参考:http://www.cnblogs.com/fnng/p/3576154.html 本文参考虫师的博客“python实现简单爬虫功能”,整理分析后抓取其他站点的图片并下载保存在本地. 抓取图片 ...

  4. P38、面试题3:二维数组中的查找

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 首先选取数组中右上角的数字 ...

  5. DSP6455 DSP/BIOS中断配置问题(是否需要ECM-事件组合以及实例)

    2013-06-20 21:08:48 中断的配置有两种常用的方式: 一是通过CSL提供的API进行配置,这种方法相对DSP/BIOS偏底层,也比较麻烦:这种方法要求对中断系统的工作方式很清楚. 二是 ...

  6. Exynos 4412的启动过程分析[2]

    做实验时我们是把 bin 文件烧入SD卡,比如前面做的汇编流水灯实验. 问:是谁把这些指令从 SD 卡读出来执行? 答:是固化在芯片内部ROM上的代码---它被称为iROM ,iROM是厂家事先烧写在 ...

  7. 【HDOJ】3828 A + B problem

    显然需要贪心,重叠越长越好,这样最终的串长尽可能短.需要注意的是,不要考虑中间结果,显然是个状态dp.先做预处理去重,然后求任意一对串的公共长度. /* 3828 */ #include <io ...

  8. OM Price Lists

      --select * --from org_organization_definitions; --execute fnd_client_info.set_org_context(111); -- ...

  9. Java版本的在指定目录及子目录下创建指定的文件

    和删除指定目录及子目录下名叫“xxx.txt”的所有文件一样,也是使用递归的方式实现的. 代码如下: public class Example826003 { private static FileO ...

  10. [POJ 1674] Sorting by Swapping

    Sorting by Swapping Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9514   Accepted: 50 ...