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 0c5dea7

Browse files
solved algorithm daily challege, search rotated sorted array
1 parent ae46d10 commit 0c5dea7

File tree

1 file changed

+88
-0
lines changed
  • LintCode/0062_Search_Rotated_Sorted_Array

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <bits/stdc++.h>
2+
#include <gtest/gtest.h>
3+
using namespace std;
4+
5+
6+
//// START
7+
/*
8+
## 0062 Search Rotated Sorted Array
9+
10+
*/
11+
12+
class Solution {
13+
public:
14+
/**
15+
* @param A: an integer rotated sorted array
16+
* @param target: an integer to be searched
17+
* @return: an integer
18+
*/
19+
int search(vector<int> &A, int target) {
20+
if (A.empty()) return -1;
21+
// write your code here
22+
int head = A[0],tail = A[A.size()-1];
23+
int left = 0 ,right = A.size()-1;
24+
if (target >= head){
25+
while(left < right){
26+
int mid = left + (right-left)/2;
27+
if (A[mid] < target && A[mid] >= head){
28+
left = mid+1;
29+
}else{
30+
right = mid;
31+
}
32+
}
33+
if (left < A.size()){
34+
if (A[left] != target) return -1;
35+
return left;
36+
}else{
37+
return -1;
38+
}
39+
}else{
40+
while(left < right){
41+
int mid = left + (right-left)/2;
42+
if (A[mid] < target && A[mid] <= tail || A[mid] >= head ){
43+
left = mid+1;
44+
}else{
45+
right = mid;
46+
}
47+
}
48+
if (left < A.size()){
49+
if (A[left] != target) return -1;
50+
return left;
51+
}else{
52+
return -1;
53+
}
54+
}
55+
}
56+
};
57+
58+
59+
//// END
60+
struct T{
61+
62+
};
63+
64+
TEST(Solution,test){
65+
T ts[] = {
66+
{
67+
68+
},
69+
{
70+
71+
},
72+
73+
};
74+
75+
76+
for (T t : ts){
77+
Solution solution;
78+
79+
}
80+
}
81+
82+
int main() {
83+
testing::InitGoogleTest();
84+
85+
return RUN_ALL_TESTS();
86+
}
87+
88+

0 commit comments

Comments
(0)

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