leetcode 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 return -1.

You may assume no duplicate exists in the array.

[Solution]

1) A[middle] > A[left], 说明 A[left, middle]有序。

2) A[middle] < A[left], 说明 A[middle, right]有序。

 1 int search(int A[], int n, int target)
 2     {
 3         int left = 0, right = n - 1, middle;
 4
 5         while (left <= right)
 6         {
 7             middle = (left + right) >> 1;
 8             if (A[middle] == target)
 9                 return middle;
10             if (A[middle] > A[left])
11             {
12                 if (target >= A[left] && target < A[middle])
13                     right = middle - 1;
14                 else
15                     left = middle + 1;
16             }
17             else if (A[middle] < A[left])
18             {
19                 if (target > A[middle] && target <= A[right])
20                     left = middle + 1;
21                 else
22                     right = middle - 1;
23             }
24             else
25             {
26                 middle = right;
27                 break;
28             }
29         }
30
31         return A[middle] == target ? middle : -1;
32     }
时间: 2024-10-22 02:45:57

leetcode 33. Search in Rotated Sorted Array的相关文章

LeetCode 33 Search in Rotated Sorted Array [binary search] &lt;c++&gt;

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数的下标,如果没有,返回-1.时间复杂度限制\(O(log_2n)\) C++ 我的想法是先找到数组中最大值的位置.然后以此位置将数组一分为二,然后在左右两部分分别寻找target. 二分寻找最大值的时候,因为左半部分的数一定大于nums[l],所以n

[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. (

LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

Suppose an array sorted in ascending order 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

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

LeetCode 33 Search in Rotated Sorted Array(在旋转排序数组中搜索)(*)

翻译 假定一个数组在一个我们预先不知道的轴点旋转. 例如,0 1 2 4 5 6 7可能会变为4 5 6 7 0 1 2. 给你一个目标值去搜索,如果找到了则返回它的索引,否则返回-1. 你可以假定没有重复的元素存在于数组中. 原文 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

LeetCode 33 Search in Rotated Sorted Array (C,C++,Java,Python)

Problem: 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

19.2.2 [LeetCode 33] Search in Rotated Sorted Array

Suppose an array sorted in ascending order 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.

33. Search in Rotated Sorted Array &amp;&amp; 81. Search in Rotated Sorted Array II &amp;&amp;

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

Java for LeetCode 081 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. 解题思路: 参考Java for LeetCode 033 Search in Rota