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 Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits

Thanks for your feedback.

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     public void recoverRotatedSortedArray(ArrayList<Integer> nums)
 2     {
 3         // write your code
 4         if(nums.size()==0||nums.size()==1) return;
 5         int i =0;
 6         for(i=0; i<nums.size()-1;i++)
 7         {
 8             if(nums.get(i)>nums.get(i+1)) break;
 9         }
10         if(i == nums.size()-1) return;
11         reverse(nums,0,i);
12         reverse(nums,i+1,nums.size()-1);
13         reverse(nums,0,nums.size()-1);
14         return;
15
16
17     }
18
19
20     public void reverse(ArrayList<Integer> nums, int left, int right)
21     {
22         while(left<right)
23         {
24             int temp = nums.get(left);
25             nums.set(left,nums.get(right));
26             nums.set(right,temp);
27             left++;
28             right--;
29
30         }
31
32         return;
33     }
时间: 2024-08-02 04:52:09

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

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 ro

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] 解题: 开始我想,先找到中间的临界点,然后在排序,临界点找到了,排序不知道怎么搞了,在这里,看到了很好的方法,前半部分逆序,后半部分逆序,整

[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

LeetCode: Search in Rotated Sorted Array

LeetCode: 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, othe

【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

LeetCode33 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

Leetcode[154]-Find Minimum in Rotated Sorted Array II

Link: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is

[LeetCode] Search in Rotated Sorted Array I (33) &amp;&amp; II (81) 解题思路

33. 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