-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearch in Rotated Sorted Array 2.cpp
More file actions
39 lines (36 loc) · 1.02 KB
/
Search in Rotated Sorted Array 2.cpp
File metadata and controls
39 lines (36 loc) · 1.02 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
class Solution {
public:
bool search(vector<int>& nums, int target) {
if(!nums.size())
return false;
if(nums.size() == 1)
return (nums[0] == target);
int low = 0, high = nums.size() - 1, pivot, mid;
while(low <= high)
{
mid = (low + high) / 2;
if(nums[mid] == target || nums[low] == target || nums[high] == target)
return true;
else if(nums[mid] > nums[low])
{
if(target > nums[low] && target < nums[mid])
high = mid - 1;
else
low = mid + 1;
}
else if(nums[mid] < nums[high])
{
if(target > nums[mid] && target < nums[high])
low = mid + 1;
else
high = mid - 1;
}
else
{
low++;
high--;
}
}
return false;
}
};