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 II
 1 public class Solution {
 2     //rotated array all is <= 
 3     public boolean search(int[] A, int target) {
 4         if(A.length<=0){return false;}
 5         int start = 0, end = A.length-1;
 6         while(start<=end){
 7             int mid = (start+end)/2;
 8             if(A[mid]==target) return true;
 9             if(A[mid]>A[start]){
10                 if(target>=A[start] && target<=A[mid]){
11                     end = mid-1;
12                 }
13                 else{
14                     start = mid+1;
15                 }
16             }
17             else if(A[mid]<A[start]){
18                 if(target>=A[mid] && target<=A[end]){
19                     start = mid+1;
20                 }
21                 else{
22                     end = mid-1;
23                 }
24             }
25             else{
26                 start ++;
27             }
28         }
29         return false;
30     }
31 }
View Code

Search in Rotated Sorted Array II

上一篇:Next Permutation


下一篇:[MongoDB] 安装MongoDB配置Replica Set