Leetcode-Search in Rotated Sorted Array
2014-11-21 11:27
204 查看
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
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.
Analysis:
Suppose we have three pointers:start, mid and end. There are three case for the array between start and end:
1. A[mid] < A[start]: Rotated, the maximum value is on the left of mid. If target < A[mid], then we search the left part; if target > A[mid], we then have two situations: (a) target <= A[end], we search the right part; (b) target > A[end], search the left part.
2. A[mid] > A[end]: Rotated, the maximum value is on the right of mid. target > A[mid]: search the left part; target < A[mid] && < A[start]: search the right part; target < A[mid] && >= A[start]: search the left part.
3. Otherwise: Not rotated. Just perform normal binary search.
Solution:
(i.e.,
0 1 2 4 5 6 7might 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.
Analysis:
Suppose we have three pointers:start, mid and end. There are three case for the array between start and end:
1. A[mid] < A[start]: Rotated, the maximum value is on the left of mid. If target < A[mid], then we search the left part; if target > A[mid], we then have two situations: (a) target <= A[end], we search the right part; (b) target > A[end], search the left part.
2. A[mid] > A[end]: Rotated, the maximum value is on the right of mid. target > A[mid]: search the left part; target < A[mid] && < A[start]: search the right part; target < A[mid] && >= A[start]: search the left part.
3. Otherwise: Not rotated. Just perform normal binary search.
Solution:
public class Solution { public int search(int[] A, int target) { if (A.length==0) return -1; int start = 0, end = A.length-1; int mid = -1; while (start<=end){ mid = (start+end)/2; if (A[mid]==target) return mid; if (A[mid]<A[start]){ if (target<A[mid]) end = mid-1; else if (target<=A[end]) start = mid+1; else end = mid-1; continue; } if (A[mid]>A[end]){ if (target>A[mid]) start = mid+1; else if (target>=A[start]) end = mid-1; else start = mid+1; continue; } if (target<A[mid]) end = mid-1; else start = mid+1; } return -1; } }
相关文章推荐
- [LeetCode] Search in Rotated Sorted Array II
- LeetCode : Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array 解题报告
- [leetcode] Search in Rotated Sorted Array
- [LeetCode]Search in Rotated Sorted Array
- leetcode 97: Search in Rotated Sorted Array
- LeetCode Search in Rotated Sorted Array
- [LeetCode] Search in Rotated Sorted Array II 解题报告
- [LeetCode] Search in Rotated Sorted Array
- [LeetCode] Search in Rotated Sorted Array
- [Leetcode] Search in Rotated Sorted Array
- leetcode 97: Search in Rotated Sorted Array
- LeetCode: Search in Rotated Sorted Array
- [leetcode] Search in Rotated Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array
- LeetCode : Search in Rotated Sorted Array
- leetcode 98: Search in Rotated Sorted Array II
- LeetCode_Search in Rotated Sorted Array
- [LeetCode] Search in Rotated Sorted Array II
- LeetCode —— Search in Rotated Sorted Array