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 ...
随机推荐
- MemcacheQ安装及使用
一.MemcacheQ安装记录1.安装libevent查看是否已经安装了libeventrpm -qa|grep libevent如果没有安装使用yum安装yum install libevent l ...
- simplify the design of the hardware forming the interface between the processor and thememory system
Computer Systems A Programmer's Perspective Second Edition Many computer systems place restrictions ...
- OmniThreadLibrary 3.03b发布了
虽然版本号升的不大,但这也是一个重要的版本.作者发现了一个长期存在的bug,就是建立一个线程,如果不指定线程的优先级则默认设置为idle.(正确的应是Normal) 看一下具体的改动情况: 新功能: ...
- What's Assembly - CSharp - Editor - first pass.dll? Best How to Fix Assembly - CSharp - Editor - first pass.dll Error Guide
If you've found yourself here, I'm guessing that you're getting Assembly - CSharp - Editor - first p ...
- 在Vista或更高版本Windows系统中, 获取超大图标的办法
这几天写个小东西, 需要获取系统正在运行的程序图标, 一般来说32*32就足够了, 不过既然Win7能够支持超大图标(256*256), 咱们也需要与时俱进, 说不定什么时候遇到个变态客户就有这要求了 ...
- Mongo命令行中执行CRUD
在命令行中使用mongo自带的shell命令来执行CRUD操作 首先链接到数据库 增 db.qiao.insert({"qq":1}) db.qiao.save({"qq ...
- Linux下对各种压缩文件处理
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...
- How to pass selected records from form to dilog in AX 2012
static void main(Args args) { FormDataSource formDataSource; ; if(args.record().TableId == tablenum( ...
- dedecms程序给栏目增加缩略图的方法
用织梦程序做网站,有时候因为功能需求,我们要为网站的栏目页添加缩略图功能,而dedecms又没自带这个功能,那么就需要我们来修改程序了. 这里有一个栏目添加缩略图的方法,供大家参考. 涉及到文件如下( ...
- Java学习-038-JavaWeb_007 -- JSP 动作标识 - plugin
plugin 动作时用来在 JSP 页面中加载 Java Applet 或者 JavaBean 组件,语法格式如下所示: <jsp:plugin type="bean|applet&q ...