-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchInRotatedSortedArray.cpp
66 lines (58 loc) · 1.99 KB
/
SearchInRotatedSortedArray.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
#include<bits/stdc++.h>
#include<functional>
using namespace std;
class Solution {
public:
// using while loop
int search(vector<int>& nums, int target) {
int n = nums.size(), low = 0, high = n-1;
while(low <= high){
int mid = (low + high)/2;
// check if the mid is the target
if(nums[mid] == target) return mid;
// check if left portion is sorted
if(nums[low] <= nums[mid]) {
// check if target is in left portion
if(target >= nums[low] && target <= nums[mid]) high = mid - 1;
// if not in left portion, move low to mid + 1
else low = mid + 1;
} else {
// check if target is in right portion
if(target >= nums[mid] && target <= nums[high]) low = mid + 1;
// if not in right portion, move high to mid - 1
else high = mid - 1;
}
}
// if not found
return -1;
}
// using recursion and lamda function
int searchLamda(vector<int>& nums, int target) {
int size = nums.size() - 1;
function<int(int, int)> func;
func = [&func, &nums, size, &target](int low, int high) mutable -> int {
if (low > high)
return -1;
int mid = (low + high) / 2;
if (nums[mid] == target)
return mid;
if (nums[low] <= nums[mid]) {
if (target >= nums[low] && target <= nums[mid])
return func(low, mid - 1);
return func(mid + 1, high);
}
if (target >= nums[mid] && target <= nums[high])
return func(mid + 1, high);
return func(low, mid - 1);
};
return func(0, nums.size() - 1);
}
};
int main () {
vector<int> nums = {4, 5, 6, 7, 0, 1, 2};
int target = 0;
Solution s;
int ans = s.search(nums, target);
cout << "ans = " << ans << endl;
return 0;
}