Repository navigation
Expand file tree
/
Copy pathsearchRotatedSortedArray.cpp
More file actions
106 lines (82 loc) · 2.35 KB
/
Copy pathsearchRotatedSortedArray.cpp
File metadata and controls
106 lines (82 loc) · 2.35 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Created by mi on 7/5/17.
//
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
/*
* Search in Rotated Sorted Array
* leetcode: https://leetcode.com/problems/search-in-rotated-sorted-array/#/description
* csdn: http://www.cnblogs.com/grandyang/p/4325648.html
*
* */
class Solution {
public:
int search(vector<int> &nums, int target) {
int n = nums.size();
if (n == 0) return -1;
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < nums[right]) {
if (nums[mid] <= target && nums[right] >= target)
left = mid + 1;
else
right = mid - 1;
} else {
if (nums[mid] >= target && nums[left] <= target)
right = mid - 1;
else
left = mid + 1;
}
}
return -1;
}
};
/*
* Search in Rotated Sorted Array II
* http://www.cnblogs.com/grandyang/p/4325840.html
* leetcode: https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description
* */
class Solution2 {
public:
bool search(vector<int> &nums, int target) {
int len = nums.size();
int left = 0;
int right = len - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target)
return true;
else if (nums[mid] < nums[right]) {
if (nums[mid] < target && nums[right] >= target)
left = mid + 1;
else
right = mid - 1;
} else if (nums[mid] > nums[right]) {
if (nums[left] <= target && nums[mid] > target )
right = mid - 1;
else
left = mid + 1;
} else
--right;
}
return false;
}
};
/*
int main() {
Solution *solution = new Solution();
vector<int> A = {1, 2, 3, 4, 5, 6,6};
cout << solution->search(A, 3)<<'\n';
*/
/******************************************//*
Solution2 *solution2 = new Solution2();
vector<int> B={3,5,1};
cout<<solution2->search(B,3);
cout<<'\n'<<true;
}*/