LeetCode-Search in Rotated Sorted Array II
2015-01-20 07:13
148 查看
Analysis:
For target>A[mid]:
There is only one case that we should seach [start,mid-1]: Peak is in the left, i.e., A[start]>A[mid] && target>=A[start].
For target<A[mid]:
There is only one case that we should search [mid+1,end]: Peak is in the right, i.e., A[end]<A[mid] && target<=A[end].
Solution:
For target>A[mid]:
There is only one case that we should seach [start,mid-1]: Peak is in the left, i.e., A[start]>A[mid] && target>=A[start].
For target<A[mid]:
There is only one case that we should search [mid+1,end]: Peak is in the right, i.e., A[end]<A[mid] && target<=A[end].
Solution:
public class Solution { public boolean search(int[] A, int target) { return searchRecur(A,target,0,A.length-1); } public boolean searchRecur(int[] A, int target, int start, int end){ if (start>end) return false; int mid = (start+end)/2; if (A[mid]==target) return true; if (A[start]==A[mid] && A[end]==A[mid]) return ( searchRecur(A,target,start,mid-1) || searchRecur(A,target,mid+1,end) ); if (target>A[mid]) if (A[start]>A[mid] && target>=A[start]) return searchRecur(A,target,start,mid-1); else return searchRecur(A,target,mid+1,end); else if (A[end]<A[mid] && target<=A[end]) return searchRecur(A,target,mid+1,end); else return searchRecur(A,target,start,mid-1); } }
相关文章推荐
- [LeetCode] Search in Rotated Sorted Array II
- 【Leetcode】Search in Rotated Sorted Array II
- leetcode_Search in Rotated Sorted Array II
- [leetcode] Search in Rotated Sorted Array II
- Search in Rotated Sorted Array II [LeetCode]
- LeetCode-Search in Rotated Sorted Array II
- leetcode 98: Search in Rotated Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array II
- 【leetcode】Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II 解题报告
- [Leetcode] Search in Rotated Sorted Array II
- LeetCode-Search in Rotated Sorted Array II
- leetcode Search in Rotated Sorted Array II
- LeetCode Search in Rotated Sorted Array II
- LeetCode - Search in Rotated Sorted Array II
- LeetCode : Search in Rotated Sorted Array II
- 【Leetcode】Search in Rotated Sorted Array II
- leetcode - Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II
- [LeetCode]Search in Rotated Sorted Array II