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.
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
If nums = [3,2,2,1]
and k=2
, a valid answer is 1
.
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的更多相关文章
- LintCode 373: Partition Array
LintCode 373: Partition Array 题目描述 分割一个整数数组,使得奇数在前偶数在后. 样例 给定[1, 2, 3, 4],返回[1, 3, 2, 4]. Thu Feb 23 ...
- Lintcode: Partition Array
Given an array "nums" of integers and an int "k", Partition the array (i.e move ...
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- Lintcode373 Partition Array by Odd and Even solution 题解
[题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...
- [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 ...
- 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 ...
- Partition Array into Disjoint Intervals LT915
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- 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 ...
随机推荐
- 218. The Skyline Problem
题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...
- 第三部分 overlay 学习
前文仅了解了overlay HAL的架构,下面继续看看系统层是如何调用Overlay模块. 1.测试代码 frameworks/base/libs/surfaceflinger/tests/overl ...
- poj 1459 Power Network(增广路)
题目:http://poj.org/problem?id=1459 题意:有一些发电站,消耗用户和中间线路,求最大流.. 加一个源点,再加一个汇点.. 其实,过程还是不大理解.. #include & ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- C# Process.Kill() 拒绝访问(Access Denied) 的解决方案
需求:很多时候我们需要后台运行几个Console来不停的计算数据,那么部署到客户服务器后,如果出现突发异常,程序挂掉了,那...? 解决方案:封装了一个对后台运行程序不停监测的功能,如果发现程序有异常 ...
- [CF 471C] MUH and House of Cards
C. MUH and House of Cards Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elep ...
- windows 远程桌面连接 RPi 2B
/************************************************************************* * windows 远程桌面连接 RPi 2B * ...
- 【转】JAVA中的反射机制
反射,当时经常听他们说,自己也看过一些资料,也可能在设计模式中使用过,但是感觉对它没有一个较深入的了解,这次重新学习了一下,感觉还行吧! 一,先看一下反射的概念: 主要是指程序可以访问,检测和修改它本 ...
- Android学习系列(1)--为App签名(为apk签名)
写博客是一种快乐,前提是你有所写,与人分享,是另一种快乐,前提是你有舞台展示,博客园就是这样的舞台.这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 1.签 ...
- Button 自定义(一)-shape
需求:自定义Button,使用系统自定义Shape: 效果图: 1.默认状态 2.选中状态 实现分析: 1.目录结构: 代码实现: 1.button_normal.xml <?xml versi ...