1. Given a rotated sorted array, recover it to sorted array in-place.
  2.  
  3. Example
  4. [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]
  5.  
  6. Challenge
  7. In-place, O(1) extra space and O(n) time.
  8.  
  9. Clarification
  10. What is rotated array:
  11.  
  12. - 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]

我的做法是先扫描不是ascending order的两个点,然后reverse array三次,分别是(0, i), (i+1, num.length-1), (0, num.length-1)

  1. public class Solution {
  2. /**
  3. * @param nums: The rotated sorted array
  4. * @return: The recovered sorted array
  5. */
  6. public void recoverRotatedSortedArray(ArrayList<Integer> nums) {
  7. // write your code
  8. if (nums==null || nums.size()==0 || nums.size()==1) return;
  9. int i = 0;
  10. for (i=0; i<nums.size()-1; i++) {
  11. if (nums.get(i) > nums.get(i+1)) break;
  12. }
  13. if (i == nums.size()-1) return;
  14. reverse(nums, 0, i);
  15. reverse(nums, i+1, nums.size()-1);
  16. reverse(nums, 0, nums.size()-1);
  17. }
  18.  
  19. public void reverse(ArrayList<Integer> nums, int l, int r) {
  20. while (l < r) {
  21. int temp = nums.get(l);
  22. nums.set(l, nums.get(r));
  23. nums.set(r, temp);
  24. l++;
  25. r--;
  26. }
  27. }
  28. }

Lintcode: Recover Rotated Sorted Array的更多相关文章

  1. lintcode:Recover Rotated Sorted Array恢复旋转排序数组

    题目: 恢复旋转排序数组 给定一个旋转排序数组,在原地恢复其排序. 样例 [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] 挑战 使用O(1)的额外空间和O(n)时间复杂度 ...

  2. Recover Rotated Sorted Array

    Given a rotated sorted array, recover it to sorted array in-place. Clarification What is rotated arr ...

  3. 39. recover rotated sorted array恢复旋转排序数组

    一句话思路:从左边开始的三步翻转法 一刷报错: 不理解start.end是位置随机定义的.i,j是临时变量,为start,end服务 nums.size()区别于nums.length:用于范形变量. ...

  4. LintCode Find Minimum In Rotated Sorted Array

    1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...

  5. 【Lintcode】062.Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  6. 【Lintcode】159.Find Minimum in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  7. [OJ] Find Minimum in Rotated Sorted Array II

    LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...

  8. [OJ] Find Minimum in Rotated Sorted Array

    LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

随机推荐

  1. mod_php VS mod_fastcgi

    mod_php VS mod_fastcgi 目录 什么是mod_php和mod_fastcgi 1 工作原理 1 mod_php 2 mod_fastcgi 3 mod_factcgi的三种配置方式 ...

  2. centos6.5 扩容

    #查看挂载点: df -h #显示: 文件系统 容量 已用 可用 已用%% 挂载点 /dev/mapper/vg_dc01-lv_root 47G 12G 34G % / tmpfs 504M 88K ...

  3. 蓝牙BLE 架构剖析

    一.BLE架构概述: 二.各个层

  4. 低功耗蓝牙4.0BLE编程-nrf51822开发(1)

    为了省钱,也为了提高手动能力,只买了块核心板,仿真器用的是旧的jinkv7,自己搭扩展板,DIY就这样开始了. 买这块之前做了些调查,最终选定了nrf51822,功耗低,性能强,开发难度小,虽然比TI ...

  5. sql索引组织

    select p.*, p.partition_id,  c.object_id,OBJECT_NAME(c.object_id) objectName,c.name,c.column_id,pc.m ...

  6. 【转】用树莓派搭建web服务器

    本文将详细介绍如何在树莓派上配置服务器,和<教你在Xubuntu上搭建LAMP服务器>有些类似,多了一些介绍在树莓派上的不同步骤的地方. 这种服务器的配置被称为LAMP,是最流行的服务器配 ...

  7. C++ 字符串操作常见函数

    //字符串拷贝,排除指定字符 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char * ...

  8. 转:ASP.NET MVC + EF 更新的几种方式

    1.常用 db.Entry(实体).State = EntityState.Modified;db.SaveChanges(); 2.指定更新 db.Configuration.ValidateOnS ...

  9. 长城坑爹宽带,劫持用户DNS赚取购物返利

    今天回来登录www.jd.com 打算淘点东西,登录后发现地址栏跳到 http://www.jd.com/?utm_source=click.linktech.cn&utm_medium=tu ...

  10. [LeetCode]题解(python):059-Spiral Matrix II

    题目来源 https://leetcode.com/problems/spiral-matrix-ii/ Given an integer n, generate a square matrix fi ...