Recover Rotated Sorted Array
来源:互联网 发布:matlab 矩阵元素赋值 编辑:程序博客网 时间:2024/06/15 21:05
Given a rotatedsorted array, recover it to sorted array in-place.
Clarification
What is rotated array?
- For example, the orginal array is [1,2,3,4], The rotated array of it can be [1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]
Example
[4, 5, 1, 2, 3]
-> [1, 2, 3, 4, 5]
思路是三次反转数组,第一次反转以后用一个o(n)时间找到要反转的点,然后再对index的左右两侧进行两次反转。
代码:
public void recoverRotatedSortedArray(ArrayList<Integer> nums) { // write your code if(nums == null || nums.size() == 0) return; Collections.reverse(nums); int index= -1; for(int i=0;i<nums.size()-1;i++){ if(nums.get(i)<nums.get(i+1)){ //i is the place where need to split index = i; break; } } if(index != nums.size()){ //Collcetions.reverse() reverse(nums, 0, index); reverse(nums, index+1, nums.size()-1); }else{ Collections.reverse(nums); } } private void reverse(ArrayList<Integer> nums, int i, int j){ while(i<j){ int temp = nums.get(i); nums.set(i, nums.get(j)); nums.set(j, temp); i++;j--; } }
0 0
- Recover Rotated Sorted Array
- recover-rotated-sorted-array
- Recover Rotated Sorted Array
- Recover Rotated Sorted Array
- Recover Rotated Sorted Array
- Recover Rotated Sorted Array
- [刷题]Recover Rotated Sorted Array
- lintcode:Recover Rotated Sorted Array
- lintcode Recover Rotated Sorted Array
- #39 Recover Rotated Sorted Array
- [Lintcode]Recover Rotated Sorted Array Show result
- 【LintCode】Recover Rotated Sorted Array 恢复旋转排序数组
- Recover Rotated Array
- 39.Recover Rotated Sorted Array-恢复旋转排序数组(容易题)
- L2:12 - Recover Sorted Array
- Search in Rotated Sorted Array
- Rotated sorted array binary search
- Search in Rotated Sorted Array
- 6. Your dev and test sets should come from the same distribution 你的开发集和测试集应该来自同一分布(《MACHINE LEARNING
- android中的SVG图像的各个属性意义
- 中国电科院招渗透测试工程师和逆向分析工程师
- 北航第十二届程序设计竞赛网络预赛题解
- Nth to Last Node in List
- Recover Rotated Sorted Array
- 7. How large do the dev/test sets need to be? 开发集和测试集多大合适(《MACHINE LEARNING YEARNING》翻译)
- JsonPath的使用
- linux find
- android矢量动画
- 删除所有容器
- 8. Establish a single-number evaluation metric for your team to optimize 建立一个单一数字的评估指标(MACHINE LE
- linux awk
- Mac上QT工程转为Xcode工程