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.

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

Quick Sort 一样的做法,只是有两种情况特殊处理:我第一次做的时候没有考虑到

1. all elements in nums are greater than or equal to k, l pointer never shift, should return l

2. all elements in nums are smaller than k, r pointer never shift, shoud return r+1

第一次做法(稍次)

 public class Solution {
/**
*@param nums: The integer array you should partition
*@param k: As description
*return: The index after partition
*/
public int partitionArray(ArrayList<Integer> nums, int k) {
//write your code here
if (nums==null || nums.size()==0) return 0;
int l=0, r=nums.size()-1;
while (true) {
while (l<r && nums.get(r)>=k) {
r--;
}
while (l<r && nums.get(l)<k) {
l++;
}
if (l == r) break;
swap(l, r, nums);
}
if (l==0 && nums.get(l)>=k) return r;
if (r==nums.size()-1 && nums.get(l)<k) return r+1;
return r+1;
} public void swap(int l, int r, ArrayList<Integer> nums) {
int temp = nums.get(l);
nums.set(l, nums.get(r).intValue());
nums.set(r, temp);
}
}

第二次做法(推荐): 只要l,r 都动过,l停的位置就是first index that nums[i] >= k, 一般情况return l就好了

单独讨论l或者r没有动过的情况,l没有动过的情况还是return l, r没有动过的情况return r+1

 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) {
//write your code here
if (nums==null || nums.length==0) return 0;
int l=0, r=nums.length-1;
while (true) {
while (l<r && nums[l]<k) {
l++;
}
while (l<r && nums[r]>=k) {
r--;
}
if (l == r) break;
swap(l, r, nums);
}
//if (l==0 && nums[l]>=k) return l;
if (r==nums.length-1 && nums[r]<k) return r+1;
return l;
} public void swap(int l, int r, int[] nums) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
}
}

Lintcode: Partition Array的更多相关文章

  1. LintCode "Partition Array by Odd and Even"

    One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integ ...

  2. LintCode 373: Partition Array

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

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

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

  4. 373. Partition Array by Odd and Even【LintCode java】

    Description Partition an integers array into odd number first and even number second. Example Given  ...

  5. lintcode 容易题:Partition Array by Odd and Even 奇偶分割数组

    题目: 奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 挑战 在原数组中完成,不使用额外空间. 解题: 一次快速排序就可 ...

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

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

  7. Partition Array

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

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

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

  9. [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 ...

随机推荐

  1. .gitignore 配置

    .gitignore 配置文件用于配置不需要加入版本管理的文件,配置好该文件可以为我们的版本管理带来很大的便利,以下是个人对于配置 .gitignore 的一些心得. 1.配置语法: 以斜杠“/”开头 ...

  2. 发现了一个很好的Pasical编程软件Lazarus

    下载地址:http://www.lazarus.freepascal.org/ 中文社区:http://www.fpccn.com/ 该软件有几点如下特征: 1.跨平台.体积小(只有100多M) 2. ...

  3. extjs MVC模式的个人看法

    针对一个后台管理页面是mvc模式,后台也是mvc模式下的项目,要怎么去熟悉呢? 首先以我个人的认解,先从后台的管理界面来看,会有control,model,store,view:其中先看view的代码 ...

  4. c语言学习感想

    接触c语言已经2个多月了,在这段期间按时的完成了作业,上课能够较好的听讲,因此我获得了老师奖励的小黄衫. 同时,希望自己能够学好c语言! 学习感受与心得 因为兴趣,选择了计算机这专业,我从遥远的南方来 ...

  5. JavaScript判断文件的大小

    function getFileSize(obj) {//obj 需要传入的参数为Input的对象   var objValue = obj.value; if (objValue == " ...

  6. php比较加赋值语句

    $a=-2;if ($a < 0 && $a = 1) { echo $a;} 输出1 右面的$a=1可不是条件哦,而是赋值

  7. 使用NSTimer过程中最大的两个坑

    坑1. retain cycle问题. 在一个对象中使用循环执行的nstimer时,若希望在对象的dealloc方法中释放这个nstimer,结局会让你很失望. 这个timer会导致你的对象根本不会被 ...

  8. 【转】android程序编译过程

    现在很多人想对Android工程的编译和打包进行自动化,比如建立每日构建系统.自动生成发布文件等等.这些都需要我们对Android工程的编译和打包有一个深入的理解,至少要知道它的每一步都做了什么,需要 ...

  9. Qt中单例模式的实现(4种方法)

    最简单的写法: 12345 static MyClass* MyClass::Instance(){ static MyClass inst; return &inst;} 过去很长一段时间一 ...

  10. 添加事件(jquery)

    对盒子内部的盒子添加跟本身盒子相同的事件的时候,需要小心谨慎一点. 诸如: 从表象上看似乎没有什么太大问题,但是却存在一个致命的问题,就是每次点击box的时候,都会给test添加一个点击事件,而添加的 ...