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

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

有个错误解法,如下:

public class Solution {
/**
*@param nums: The integer array you should partition
*@param k: As description
*return: The index after partition
*/
public int partitionArray(int[] nums, int k) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
while (left < right && nums[left] < k) {
left++;
}
while (left < right && nums[right] >= k){
right--;
}
if (left >= right) {
break;
}
swap(nums, left, right);
left++;
right--;
}
return left;
}
public void swap(int[] nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
}

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

正确解法如下:

public class Solution {
/**
*@param nums: The integer array you should partition
*@param k: As description
*return: The index after partition
*/
public int partitionArray(int[] nums, int k) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
while (left <= right && nums[left] < k) {
left++;
}
while (left <= right && nums[right] >= k){
right--;
}
if (left > right) {
break;
}
swap(nums, left, right);
left++;
right--;
}
return left;
}
public void swap(int[] nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
}

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

public class Solution {
/**
*@param nums: The integer array you should partition
*@param k: As description
*return: The index after partition
*/
public int partitionArray(int[] nums, int k) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
while (left < nums.length && nums[left] < k) {
left++;
}
while (right >= 0 && nums[right] >= k){
right--;
}
if (left >= right) {
break;
}
swap(nums, left, right);
left++;
right--;
}
return left;
}
public void swap(int[] nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
}

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

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. CityEngine 2013部署安装

    安装环境: windows8.1 专业版 已安装arcgis10.2 2的授权方式也同样分为:单机许可和浮动 许可.单机许可是将许可部署在本机直接使用:浮动许可是部署到服务器上通过IP地址连接,可借出 ...

  2. PHP 的面向方面编程

    面向方面编程(AOP)对于PHP来说是一个新的概念.现在PHP对于 AOP 并没有官方支持,但有很多扩展和库实现了这个特性.本课中,我们将使用 Go! PHP library 来学习 PHP 如何进行 ...

  3. 基于Android Studio搭建hello world工程

    基于Android Studio搭建hello world工程 版本:ANDROID STUDIO V0.4.6 This download includes: ·        Android St ...

  4. uva1658 admiral

    费用流. 裸的拆点最小费用流,一跑就行. 核弹预警,为何wa20多发.build函数一定要返回true...... 太可怕了 #include<cstdio> #include<al ...

  5. unity3d中获得物体的尺寸(size)

    1:获得诸如Plane.Cube的size.    1):可以为它们添加Collider,然后使用XXX.collider.bounds.size;该方法获得的size和缩放比例有关,是一一对应的,缩 ...

  6. 手动启动angular

    关于手动启动 angular 的问题 angular核心原理解析1:angular自启动过程 angular.element(document).ready(function() { angular. ...

  7. 卸载sqlserver数据库参考

    这多半是以前安装过MSSQL,没卸载干净导致的. 解决方法如下: 一.先把MSSQL卸载干净 1.在添加删除程序中把SQL Server卸载 :2.把Microsoft SQL Server文件夹整个 ...

  8. Weblogic11g安装

    我们在64位的服务器上为提高性能要安装64位的weblogic.经常在网上看到有人问,weblogic有64位的么?weblogic需要破解么? weblogic有专门的64位版本,这里安装的是web ...

  9. Hibernate4.x之映射关系--单向一对多

    在领域模型中,类与类之间最普遍的关系就是关联关系在UML中,关联是有方向的 以Customer和Order为例:一个用户能发出多个订单,而一个订单只能属于一个客户.从Order到Customer的关联 ...

  10. jquery 图片上传本地预览V1.2

    基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari 代码进行小小的压缩 如果看源码 自己解压就行了 版本已升级  修复jquery版本问题  支持任意jqu ...