Skip to content

Commit 591c5e1

Browse files
committed
Search In Rotated Sorted Arry Solution
1 parent 66f9809 commit 591c5e1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int search(vector<int>& nums, int target) {
4+
int start = 0;
5+
int end = nums.size() - 1;
6+
7+
while(start <= end){
8+
int mid = start + (end - start) / 2;
9+
10+
if(nums[mid] == target)
11+
return mid;
12+
13+
if(nums[start] <= nums[mid]){
14+
if(nums[start] <= target && nums[mid] > target)
15+
end = mid - 1;
16+
else
17+
start = mid + 1;
18+
}else{
19+
if(nums[mid] < target && nums[end] >= target)
20+
start = mid + 1;
21+
else
22+
end = mid - 1;
23+
}
24+
}
25+
26+
return -1;
27+
}
28+
};

0 commit comments

Comments
 (0)