Lintcode Recover Rotated Sorted Array

Given a rotated sorted array, recover it to sorted array in-place.

Example

[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]

Challenge

In-place, O(1) extra space and O(n) time.

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]



解题思路:

技巧题:知道就知道题,不知道就拉倒题。

找到分割位置,什么时候出现下降就是分割位置。

三步翻转法:

  1. [1,2,3]翻转一下变成[3,2,1]
  2. [4,5]翻转变成[5,4]
  1. 最后全部翻转一下就得到。

相关类似题目:

  1. Rotate String: abcdefg, offset = 3 -> efgabcd

方法:三步翻转法:

(1) adcd efg

(2) dcda gfe

(3) efgabcd

  1. Rotate Words List: I love you -> you love I

方法:二步翻转法:

(1)全部翻转

(2)根据空格每个单词翻转



Java code:

public class Solution {
    /**
     * @param nums: The rotated sorted array
     * @return: void
     */
    public void recoverRotatedSortedArray(ArrayList<Integer> nums) {
        // write your code
        for(int i = 0; i < nums.size() - 1 ; i++) {
            if(nums.get(i) > nums.get(i+1)) {
                reverse(nums, 0, i);
                reverse(nums, i+1, nums.size()-1);
                reverse(nums, 0 , nums.size()-1);
                return;
            }
        }

    }
    public void reverse(ArrayList<Integer> nums, int start, int end) {
        for(int i = start, j = end; i< j; i++, j--) {
            int temp = nums.get(i);
            nums.set(i, nums.get(j));
            nums.set(j, temp);
        }
    }
}


Reference:

1. http://www.jiuzhang.com/solutions/recover-rotated-sorted-array/

时间: 2024-10-24 15:34:52

Lintcode Recover Rotated Sorted Array的相关文章

[lintcode easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin

lintcode 容易题:Recover Rotated Sorted Array恢复旋转排序数组

题目: 恢复旋转排序数组 给定一个旋转排序数组,在原地恢复其排序. 样例 [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] 挑战 使用O(1)的额外空间和O(n)时间复杂度 说明 什么是旋转数组? 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3] 解题: 开始我想,先找到中间的临界点,然后在排序,临界点找到了,排序不知道怎么搞了,在这里,看到了很好的方法,前半部分逆序,后半部分逆序,整

Recover Rotated Sorted Array

Given a rotated sorted array, recover it to sorted array in-place. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Li

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (

【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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

LintCode : Find Minimum in Rotated Sorted Array

public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] num) { // write your code here // handle corner case // the idea is to find the [first] element less than num[num.

LintCode: 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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

[LintCode] 159 Find Minimum in Rotated Sorted Array

DescriptionSuppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. Notice You may assume no duplicate exists in the array. ExampleGiven [4, 5, 6, 7, 0, 1,

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q