-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200122-1.cpp
52 lines (50 loc) · 1.42 KB
/
200122-1.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
// https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
bool search(vector<int>& nums, int target) {
return searchRotated(nums, 0, nums.size() - 1, target);
}
private:
bool searchRotated(vector<int>& nums, int i, int j, int target) {
if (i > j || i >= nums.size() || j < 0) return false;
int k = (i + j) / 2;
if (nums[k] == target) return true;
if (nums[i] < nums[k]) {
if (searchOrdered(nums, i, k - 1, target)) return true;
if (searchRotated(nums, k + 1, j, target)) return true;
} else if (nums[k] < nums[j]) {
if (searchRotated(nums, i, k - 1, target)) return true;
if (searchOrdered(nums, k + 1, j, target)) return true;
} else {
if (searchRotated(nums, i, k - 1, target)) return true;
if (searchRotated(nums, k + 1, j, target)) return true;
}
return false;
}
bool searchOrdered(vector<int>& nums, int i, int j, int target) {
if (i > j || i >= nums.size() || j < 0) return false;
int k = (i + j) / 2;
if (nums[k] == target) return true;
if (nums[k] < target) {
return searchOrdered(nums, k + 1, j, target);
} else {
return searchOrdered(nums, i, k - 1, target);
}
}
};
int main()
{
Solution s;
{
vector<int> a = {2,5,6,0,0,1,2};
printf("%d\n", s.search(a, 0)); // answer: true
}
{
vector<int> a = {2,5,6,0,0,1,2};
printf("%d\n", s.search(a, 3)); // answer: false
}
return 0;
}