Lintcode: Partition Array
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的更多相关文章
- LintCode "Partition Array by Odd and Even"
		
One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integ ...
 - LintCode 373: Partition Array
		
LintCode 373: Partition Array 题目描述 分割一个整数数组,使得奇数在前偶数在后. 样例 给定[1, 2, 3, 4],返回[1, 3, 2, 4]. Thu Feb 23 ...
 - lintcode 中等题:partition array 数组划分
		
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
 - 373. Partition Array by Odd and Even【LintCode java】
		
Description Partition an integers array into odd number first and even number second. Example Given ...
 - lintcode 容易题:Partition Array by Odd and Even 奇偶分割数组
		
题目: 奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 挑战 在原数组中完成,不使用额外空间. 解题: 一次快速排序就可 ...
 - Lintcode373 Partition Array by Odd and Even solution 题解
		
[题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...
 - Partition Array
		
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...
 - [Swift]LeetCode915.将分区数组分成不相交的间隔 | Partition Array into Disjoint Intervals
		
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
 - [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 ...
 
随机推荐
- SQL Server 触发器【转】
			
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...
 - ubuntu如何开启root,如何启用Ubuntu中root帐号
			
jingyan.baidu.com/article/495ba84116104238b20ede62.html ubuntu如何开启root,如何启用Ubuntu中root帐号 | 浏览:8344 | ...
 - cvWaitKey 如果 cvNamedWindow就不会起作用
			
Have you called cvNamedWindow yet? It will not work without cvNamedWindow. http://stackoverflow.com/ ...
 - .Net 2.0自带的Json序列化、反序列化方法
			
public class JsonUtil { public static T DeserializeObject<T>(string json) { ...
 - NHibernate学习笔记
			
原文详见http://www.cnblogs.com/GoodHelper/archive/2011/02/16/nhibernate_03.html NHibernate_Demo程序框架: D ...
 - 初入C的世界
			
大家好,我叫吉贯之,来自贵州省遵义市,现就读于北京工业大学耿丹学院信息技术系计算机与科学专业,我的学号是160809127,我喜欢运动和一些电脑方面的软件操作. 应老师要求在博客园建立的博客,地址是h ...
 - 最大子序列和 o(n)
			
问题: 给定一整数序列A1, A2,... An (可能有负数),求A1~An的一个子序列Ai~Aj,使得Ai到Aj的和最大 例如:整数序列-2, 11, -4, 13, -5, 2, -5, -3, ...
 - alibaba的FastJson(高性能JSON开发包)
			
这是关于FastJson的一个使用Demo,在Java环境下验证的 class User{ private int id; private String name; public int getId( ...
 - git 初次使用
			
其实知道git很久了,也一度看了不少资料来学习指令.但是一直不明白到底我该咋办,我最疑惑的地方在于,本地代码是如何存储到远程服务器上的,那些指令在什么环境下执行,其实主要是目录问题.就是我在git s ...
 - ecshop
			
if($cat_id == '205'){ $smarty->display('cat1.dwt', $cache_id); }elseif($cat_id == '2'){ $smarty-& ...