Partition Array
来源:互联网 发布:java set叫什么 编辑:程序博客网 时间:2024/06/06 03:26
这道题用两根指针,向中间逼近。
一定要注意的是corner case时的下标处理。
/** *@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) || (nums.length == 1 && nums[0] > k)) { return 0; } if (nums.length == 1 && nums[0] > k) { return 1; } int left = 0, right = nums.length - 1; int index = 0; while (left < right) { if (nums[left] < k && nums[right] >= k) { left++; right--; index++; } else if (nums[left] >= k && nums[right] >= k) { right--; } else if (nums[left] < k && nums[right] < k) { left++; index++; } else { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; index++; } } if (left == nums.length - 1) { //1 return index++; return ++index; } else { //return index; if (nums[left] < k) { return ++index; } else return index; } //return index; }
0 0
- Partition Array
- Partition Array
- Partition Array
- Partition Array/ Sort Color
- lintcode:Partition Array
- [刷题]Partition Array
- lintcode:Partition Array
- Balanced Partition of Array
- #31 Partition Array
- Partition Array的总结
- LintCode Partition Array
- Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- Array Partition I
- leetcode 101 Symmetric Tree C++
- 时间管理,你知道多少?
- C语言《函数如何区分形参and实参》
- Linux crontab定时执行任务 命令格式与详细例子
- php中mysql问题
- Partition Array
- Ubuntu server 14.04下搭建NFS并挂载HDFS到本地+Samba服务器共享这个本地文件到Windows端
- Metal 着色语言编程指南 二三
- 中文字符串反转
- 对2016年android实习就业的一些看法
- wampserver.php
- PowerPoint2007无法将Excel图表转换为图形对象
- iOS 友盟(UM)统计操作笔记
- Android view绘制流程