forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_in_Rotated_Sorted_Array.cpp
More file actions
62 lines (56 loc) · 1.47 KB
/
Search_in_Rotated_Sorted_Array.cpp
File metadata and controls
62 lines (56 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 17, 2012
Problem: Search in Rotated Sorted Array
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
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.
Solution:
If we say the space between the minimal value and the maximal value is the 'gap'.
Then the segment contains the 'gap' must have the first element is larger than
the last element; otherwise the first element is smaller than the last element
because it's ordered.
Also be careful with the boundry conditons.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
int search(int A[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (target == A[mid]) {
return mid;
}
if (target < A[mid]) {
if (target < A[left] && A[left] <= A[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
} else {
if (target > A[mid]) {
if (target > A[right] && A[right] >= A[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
}
}
return -1;
}
};