-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33.cpp
More file actions
38 lines (34 loc) · 920 Bytes
/
33.cpp
File metadata and controls
38 lines (34 loc) · 920 Bytes
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
// Problem : 33. Search in Rotated Sorted Array
// Link : https://leetcode.com/problems/search-in-rotated-sorted-array/description/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int search(vector<int> &nums, int target) {
int lo = 0, hi = int(nums.size()) - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if ((nums[0] > target) ^ (nums[0] > nums[mid]) ^ (target > nums[mid]))
lo = mid + 1;
else
hi = mid;
}
return lo == hi && nums[lo] == target ? lo : -1;
}
};
int main() {
vector<int> data;
data.push_back(4);
data.push_back(5);
data.push_back(6);
data.push_back(7);
data.push_back(0);
data.push_back(1);
data.push_back(2);
int target = 1;
Solution ob;
cout << ob.search(data, target);
return 0;
}