Search in Rotated Sorted Array leetcode java

题目:

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.

题解:

这道题是一道常见的二分查找法的变体题。

要解决这道题,需要明确rotated sorted array的特性,那么就是至少有一侧是排好序的(无论pivot在哪,自己画看看)。接下来就只需要按照这个特性继续写下去就好。以下就以伪代码方法来说明:

  1. 如果target比A[mid]值要小
  2. 如果A[mid]右边有序(A[mid]<A[high])
  3. 那么target肯定不在右边(target比右边的都得小),在左边找
  4. 如果A[mid]左边有序
  5. 那么比较target和A[low],如果target比A[low]还要小,证明target不在这一区,去右边找;反之,左边找。
  6. 如果target比A[mid]值要大
  7. 如果A[mid]左边有序(A[mid]>A[low])
  8. 那么target肯定不在左边(target比左边的都得大),在右边找
  9. 如果A[mid]右边有序
  10. 那么比较target和A[high],如果target比A[high]还要大,证明target不在这一区,去左边找;反之,右边找。

以上实现代码如下所示:

1    public int search(int [] A,int target){
 2        if(A==null||A.length==0)
 3          return -1;
 4         
 5        int low = 0;
 6        int high = A.length-1;
 7       
 8        while(low <= high){
 9            int mid = (low + high)/2;
10            if(target < A[mid]){
11                if(A[mid]<A[high])//right side is sorted
12                  high = mid - 1;//target must in left side
13                else
14                  if(target<A[low])//target<A[mid]&&target<A[low]==>means,target cannot be in [low,mid] since this side is sorted
15                     low = mid + 1;
16                  else 
17                     high = mid - 1;
18            }else if(target > A[mid]){
19                if(A[low]<A[mid])//left side is sorted
20                  low = mid + 1;//target must in right side
21                else
22                  if(target>A[high])//right side is sorted. If target>A[high] means target is not in this side
23                     high = mid - 1;
24                  else
25                     low = mid + 1;
26            }else
27              return mid;
28        }
29        
30        return -1;
31 }

Reference:http://www.cnblogs.com/ider/archive/2012/04/01/binary_search.html

Search in Rotated Sorted Array leetcode java

时间: 2024-11-08 20:17:40

Search in Rotated Sorted Array leetcode java的相关文章

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

Search in Rotated Sorted Array -- leetcode

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

Find Minimum in Rotated Sorted Array leetcode java

题目: 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). Find the minimum element. You may assume no duplicate exists in the array. 解题思路: 首先假设一个sorted没有rotated的数组[1,2,3],假设我们通过一个

Search in Rotated Sorted Array——LeetCode

这道题在LeetCode上是Hard类的,首先声明,代码是别人的,我只是解释一下. class Solution { public: /* 如果数组是有序的,那么大家都会想到并使用二分搜索,但本题设置了这样一个障碍:数组是rotate过的.这样,原先有序的数组就变成了局部有序:(假设)分为两个有序的子数组,并且左边子数组中的元素都大于右边子数组中的元素.这时,采用二分搜索,需要判断数组nums[left..right]和下标mid分别处于什么位置.分为以下2种情况: 1.nums[mid] >=

Find Minimum in Rotated Sorted Array leetcode

原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! 1 class Solution { 2 public: 3 int findMin(vector<int>& nums) { 4 if (nums.empty()) 5 return -1; 6 int res = find(nums, 0, nums.size()-1);//好神奇,第二个参数无论减或者不减1,都不影响该题的结果 7 if (res == -

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

Search in Rotated Sorted Array II leetcode java

题目: 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][Java] 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 Ar

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