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

说明:

1)已排序数组查找采用二分查找

2)关键找到临界点

实现:

一、我的代码:

     

 1 class Solution {
 2 public:
 3     int search(int A[], int n, int target) {
 4         if(n==0||n==1&&A[0]!=target) return -1;
 5         if(A[0]==target) return 0;
 6         int i=1;
 7         while(A[i-1]<A[i])  i++;
 8
 9         int pre=binary_search(A,0,i,target);
10         int pos=binary_search(A,i,n-i,target);
11         return pre==-1?pos:pre;
12     }
13 private:
14     int binary_search(int *B,int lo,int len,int goal)
15     {
16         int low=lo;
17         int high=lo+len-1;
18         while(low<=high)
19         {
20             int middle=(low+high)/2;
21             if(goal==B[middle])//找到,返回index
22                return middle;
23             else if(B[middle]<goal)//在右边
24                low=middle+1;
25             else//在左边
26                high=middle-1;
27         }
28         return -1;//没有,返回-1
29     }
30 };

二、网上开源代码:

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

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找),布布扣,bubuko.com

时间: 2024-10-06 00:07:19

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 d

[LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. Search in Rotated Sorted Array (Hard) 链接: 题目:https://leetcode.com/problems/search-in-rotated-sorted-array/ 代码(github):https://github.com/illuz/leetcode 题

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

LeetCode 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. Java Solution ( refer to my blog LeetCode So

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 --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

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之Search in Rotated Sorted Array,剑指offer之旋转数组的最小数字

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