Skip to content

Commit

Permalink
Search In Rotated Sorted Array
Browse files Browse the repository at this point in the history
  • Loading branch information
wonderli committed Oct 31, 2013
1 parent 32d1ceb commit 5012925
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions LeetCode2/SearchInRotatedSortedArrayII/Solution.java
@@ -0,0 +1,25 @@
import java.util.*;
public class Solution{
public boolean search(int[] A, int target) {
if(A == null || A.length == 0) return false;
int len = A.length;
return helper(0, len-1, A, target);
}
public boolean helper(int l, int r, int A[], int target){
if(l > r) return false;
int mid = l + (r - l)/2;
if(A[mid] == target) return true;
else if(A[l] <= A[mid]){
if(A[l] <= target && target <= A[mid]){
return helper(l, mid-1, A, target);
}else return helper(l, mid-1, A, target) || helper(mid+1, r, A, target);
}else{
if(A[mid] <= target && target <= A[r]){
return helper(mid+1, r, A, target);
}else return helper(l, mid-1, A, target) || helper(mid+1, r, A, target);
}
}
public static void main(String args[]){
Solution sol = new Solution();
}
}

0 comments on commit 5012925

Please sign in to comment.