【leetcode】Search in Rotated Sorted Array

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

利用二分法,

如果left<A[mid],说明从左边left开始到mid是有序的

如果left>A[mid],说明右边从mid开始,到right是有序的

如果left=A[mid],则说明left和mid在同一个位置,如果A[mid]!=target则下一步中left=left+1;

 1 class Solution {
 2 public:
 3     int search(int A[], int n, int target) {
 4
 5         int left=0;
 6         int right=n-1;
 7         int mid;
 8         while(left<=right)
 9         {
10             mid=(left+right)/2;
11
12             if(A[mid]==target) return mid;
13
14             if(A[left]<A[mid])//left
15             {
16                 if(A[left]<=target&&target<A[mid])
17                 {
18                     right=mid-1;
19                 }
20                 else
21                 {
22                     left=mid+1;
23                 }
24             }
25             else if(A[left]>A[mid])//right
26             {
27                 if(A[mid]<target&&target<=A[right])
28                 {
29                     left=mid+1;
30                 }
31                 else
32                 {
33                     right=mid-1;
34                 }
35             }
36             else
37             {
38                 //最后一个判断语句可以一起放到第一个语句里,if(A[left]<=A[mid])
39                 left=mid+1;
40             }
41
42         }
43         return -1;
44     }
45 };
时间: 2024-08-02 00:03:47

【leetcode】Search in Rotated Sorted Array的相关文章

【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,在

【leetcode】Search in Rotated Sorted Array II

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. 与I类似,只是在二分搜

【LeetCode】Search in Rotated Sorted Array II (2 solutions)

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. 解法一:顺序查找 cl

【LeetCode】Search in Rotated Sorted Array (3 solutions)

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 retu

【leetcode】Search in Rotated Sorted Array II(middle)☆

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. 我的思路: 太混乱了 不提了.注意关键区分依据 排好序的一定是从小到大的 看大神的吧: b

【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 d

【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 d

【Leetcode】 Search in Rotated Sorted Array

题目链接:https://leetcode.com/problems/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 th

【leetcode】Search in Rotated Sorted Array (hard)

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