[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 7might become4 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 duplicate exists in the array.

题意:在以某点旋转以后的有序数租中找目标值。数组中没有重复的元素

思路:刚开始是否是求旋转之后(或者之前的)的目标值在数组中的下标,然后就写了一个遍历全数组的简单方法,发现是求现数组的下标(即旋转之后的)。当然若是直接遍历一遍数组,也可以找到,时间复杂为O(n)。题目给的原意不是这样的。所以尝试二分查找,发现当中间位置所对应的值小于最右边时,则,右边的是非降序列,此时只要判断目标值是否在这个区间内(如何判断?因为这段是非降的,所以只要取其首尾两个值和目标值对比即可),若在这个区间内,则对此区间进行下一次二分查找,若不在,则对左边进行二分查找;同理当中间值大于最右边值时,左边为非降序的,剩下的一样。

代码如下:

 1 class Solution {
 2 public:
 3     int search(int A[], int n, int target)
 4     {
 5         if(n==0)    return -1;
 6         int lo=0,hi=n-1;
 7         while(lo<=hi)
 8         {
 9             int mid=(lo+hi)/2;
10             if(A[mid]==target)
11                 return mid;
12             else if(A[mid]<A[hi])
13             {
14                 if(A[mid]<target&&A[hi]>=target)
15                     lo=mid+1;
16                 else
17                     hi=mid-1;
18             }
19             else
20             {
21                 if(A[lo]<=target&&A[mid]>target)
22                     hi=mid-1;
23                 else
24                     lo=mid+1;
25             }
26         }
27         return -1;
28     }
29 };
时间: 2024-11-05 11:24:09

[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, otherwise return -1. You may assume no duplic

LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 . 你可以假设数组中不存在重复的元素. 你的算法时间复杂度必须是 O(log n) 级别. 英文:Suppose an array sorted i

[CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. EXAMPLE Input: find 5 in {15, 16, 19, 20, 2

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, otherwise return -1. You may assume no du

[LeetCode] Search in Rotated Sorted Array [35]

题目 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 dup

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

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: 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 arr

[LeetCode] Search in Rotated Sorted Array II [36]

题目 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

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. 原题链接:https://oj.leetcode.com/problems/search