forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
76 lines (62 loc) · 1.84 KB
/
main.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/// Source : https://leetcode.com/problems/search-in-rotated-sorted-array/description/
/// Author : liuyubobobo
/// Time : 2018-06-22
#include <iostream>
#include <vector>
using namespace std;
/// Binary Search
/// Time Complexity: O(logn)
/// Space Complexity: O(1)
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0, r = nums.size() - 1;
while(l <= r){
//cout << "l : " << l << " r : " << r << endl;
int mid = l + (r - l) / 2;
if(nums[mid] == target)
return mid;
//cout << "mid : " << mid << endl;
if(target < nums[mid]){
if(nums[l] <= nums[r] || nums[mid] < nums[l])
r = mid - 1;
else if(target >= nums[l])
r = mid - 1;
else
l = mid + 1;
}
else{ // target > nums[mid]
if(nums[l] <= nums[r] || nums[mid] > nums[r])
l = mid + 1;
else if(target >= nums[l])
r = mid - 1;
else
l = mid + 1;
}
}
return -1;
}
};
int main() {
vector<int> nums1 = {4, 5, 6, 7, 0, 1, 2};
int target1 = 0;
cout << Solution().search(nums1, target1) << endl;
// 4
vector<int> nums2 = {4, 5, 6, 7, 0, 1, 2};
int target2 = 3;
cout << Solution().search(nums2, target2) << endl;
// -1
vector<int> nums3 = {1, 3};
int target3 = 3;
cout << Solution().search(nums3, target3) << endl;
// 1
vector<int> nums4 = {5, 1, 3};
int target4 = 5;
cout << Solution().search(nums4, target4) << endl;
// 0
vector<int> nums5 = {4, 5, 6, 7, 8, 1, 2, 3};
int target5 = 8;
cout << Solution().search(nums5, target5) << endl;
// 4
return 0;
}