Skip to content

Commit b08f10d

Browse files
authored
Merge pull request kothariji#763 from Tusharr0305/patch-1
Create 33.leetcode_search_in_rotated_array.cpp
2 parents 5b06a37 + 3c48033 commit b08f10d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
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>& A, int target) {
4+
int start = 0;
5+
int end = A.size() - 1;
6+
while (start <end) {
7+
int mid = start+(end-start) / 2;
8+
if (A[mid] == target)
9+
return mid;
10+
11+
if (A[start] <= A[mid]) {
12+
if (target >= A[start] && target < A[mid]) {
13+
end= mid - 1;
14+
} else {
15+
start = mid + 1;
16+
}
17+
} else {
18+
if (target > A[mid] && target <= A[end]) {
19+
start = mid + 1;
20+
} else {
21+
end= mid - 1;
22+
}
23+
}
24+
}
25+
return A[start] == target ? start : -1;
26+
}
27+
};

0 commit comments

Comments
 (0)