-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path033_searchInRotatedSortedArray.cpp
More file actions
35 lines (31 loc) · 1.15 KB
/
Copy path033_searchInRotatedSortedArray.cpp
File metadata and controls
35 lines (31 loc) · 1.15 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
// Sourse : https://leetcode.com/problems/search-in-rotated-sorted-array/
// Difficulty : Hard
/***********************************************************************
*
* 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.
*
**********************************************************************/
class Solution {
public:
// if ans is at right of m
// ----------- rot -- m - ans -- : n[0] > n[m], n[0] > T, n[m] < T
// ----- m --- rot ---- ans ---- : n[0] < n[m], n[0] > T, n[m] > T
// - m - ans - rot ------------- : n[0] < n[m], n[0] < T, n[m] < T
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size()-1;
while (l < r) {
int mid = (l + r) / 2;
if ((nums[0] > target) ^ (nums[0] > nums[mid]) ^ (target > nums[mid]))
l = mid + 1;
else
r = mid;
}
return nums[l] == target ? l : -1;
}
};