LintCode-----31.数组划分
2017-08-29 16:00
204 查看
原题目
使用快排的交换思想就行。值得注意的是,大于等于的数字一定要到左边。但是快排中等于是可以不动的。
使用快排的交换思想就行。值得注意的是,大于等于的数字一定要到左边。但是快排中等于是可以不动的。
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) { if (nums == null || nums.length == 0) { return 0; } int i = 0; int j = nums.length - 1; int result = 0; while (i != j) { while (nums[j] >=k && i < j) { j--; } while (nums[i] <k && i < j) { i++; } if (i < j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } boolean flag = false; for (int m = 0; m < nums.length; m++) { if (nums[m] >= k) { flag = true; result = m; break; } } if (!flag) { result = nums.length; } return result; } }
相关文章推荐
- lintcode 中等题:partition array 数组划分
- lintcode&九章算法——Lintcode No.31 划分数组 ? 待解决
- Lintcode 数组划分
- lintcode之数组划分
- lintcode--子数组之和
- lintcode 42 最大子数组 II 解析
- lintcode循环数组之连续子数组求和
- lintcode: 把排序数组转换为高度最小的二叉搜索树
- LintCode 两数组的交
- lintcode——链表划分
- LINTCODE——最大子数组III
- 排颜色问题——数组 leetcode lintcode
- lintcode-44-最小子数组
- lintcode 44.最小子数组
- LINTCODE——将数组重新排序以构造最小值
- LintCode(easy)两数组的交
- LintCode-删除排序数组中的重复数字
- Lintcode——两数组的交
- LintCode 6.合并排序数组 II
- Lintcode:合并排序数组