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 ...
随机推荐
- Kafka 0.10.0
2.1 Producer API We encourage all new development to use the new Java producer. This client is produ ...
- Jsoncpp 数组的使用
JsonCpp 是一个C++用来处理JSON 数据的开发包.下面讲一下怎么使用JsonCpp来序列化和反序列化Json对象,以实际代码为例子. 反序列化Json对象 比如一个Json对象的字符串序列如 ...
- java Direct Buffer
public static ByteBuffer allocate (int capacity) //性能低于下面的Direct,因为是把内存建立在JVM堆上,容易被GC回收,可能需要多次 ...
- java CyclicBarrier
import java.io.IOException; import java.util.Random; import java.util.concurrent.BrokenBarrierExcept ...
- addevent兼容函数 && 阻止默认行为 && 阻止传播
function addEvent(a, b, c, d) { a.addEventListener ? a.addEventListener(b, c, d) : a.attachEvent(&qu ...
- 数据库留言板例题:session和cookie区别
session和cookie区别: <?php session_start(); //session_start();必须写在所有的php代码前边 ?> <!DOCTYPE html ...
- 后半部分样式和JS前半部分脚本语言
样式 剩余样式: 1.<div style=display:"none"></div>:nono 是隐藏该元素内容,block是显示该元素内容 2.< ...
- Android 4.2蓝牙介绍
蓝牙一词源于公元十世纪丹麦国王HaraldBlatand名字中的Blatand.Blatand的英文之意就是Blue tooth.这是因为这位让丹麦人引以为傲的国王酷爱吃蓝莓以至于牙龈都被染成蓝色.由 ...
- php--如何解决网站分页导致的SEO问题
如何解决网站分页导致的SEO问题 分页(pagination)是一种自动分页机制,可以将移动Web窗体中的内容分割成一组组较小的页进行呈现,以适合于特定的设备,该机制还呈现可用于浏览到其他页的用户界面 ...
- socketserve及其应用
1.cmd命令 利用socket socket服务端 import socket import subprocess s = socket.socket() s.bind(("127.0.0 ...