Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 14b157f

Browse files
commit
1 parent 5346abf commit 14b157f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎33. Search in Rotated Sorted Array.cpp

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /