31. 数组划分
2018-03-06 21:11
176 查看
给出一个整数数组 nums 和一个整数 k。划分数组(即移动数组 nums 中的元素),使得:所有小于k的元素移到左边
所有大于等于k的元素移到右边
返回数组划分的位置,即数组中第一个位置 i,满足 nums[i] 大于等于 k。样例给出数组 nums =
所有大于等于k的元素移到右边
返回数组划分的位置,即数组中第一个位置 i,满足 nums[i] 大于等于 k。样例给出数组 nums =
[3,2,2,1]和 k =
2,返回
1.
public class Solution { /** * @param nums: The integer array you should partition * @param k: An integer * @return: The index after partition */ public int partitionArray(int[] nums, int k) { // write your code here int i = 0, j = nums.length-1; while (i < j) { while (i < nums.length && nums[i] < k) { i++; } while (j >= 0 && nums[j] >= k) { j--; } if (i > j) { break; } if (i > nums.length) { break; } if (j < 0) { break; } int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } return i; } }
相关文章推荐
- <LeetCode> 题31:数组划分
- LintCode-----31.数组划分
- lintcode-31数组划分
- 31. 数组划分
- lintcode-31-数组划分
- 31 - 数组划分
- 剑指Offer面试题31(java版):连续子数组的最大和
- 数组的定义与内存的划分
- 剑指offer-面试题31-连续子数组的最大和
- LintCode-数组划分
- 面试题 31: 求子数组的最大和
- 划分数组问题
- 面试题31连续数组的最大和
- 剑指offer——面试题31:连续子数组的最大和
- 百度笔试题:一个已经排序好的很大的数组,现在给它划分成m段,每段长度不定,段长最长为k,然后段内打乱顺序,请设计一个算法对其进行重新排序
- 开源编译器学习笔记04(VC6 动态节点数组——AddNode)——2014_1_31
- 剑指Offer-31-连续子数组的最大和
- C/C++把字符串划分为二维字数组,2种分割方法
- 剑指offer:(31)时间效率 :连续子数组最大和
- 面试OR笔试42——划分数组