Skip to content

Commit c94252a

Browse files
committed
solved check if array is sorted and rotated
1 parent 7835df7 commit c94252a

File tree

1 file changed

+70
-0
lines changed
  • LeetCode/CheckIfArrayIsSortedAndRotated

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <bits/stdc++.h>
2+
#include <gtest/gtest.h>
3+
4+
using namespace std;
5+
6+
7+
8+
class Solution {
9+
private:
10+
enum status {
11+
front,
12+
end,
13+
};
14+
15+
16+
public:
17+
bool check(vector<int>& nums) {
18+
if (nums.empty()) return true;
19+
status s = front;
20+
int last = nums[0];
21+
int index = 0;
22+
for (int i = 1 ;i < nums.size();i++){
23+
int n = nums[i];
24+
if (s == front){
25+
if (n < last){
26+
s = end;
27+
index = i;
28+
}
29+
}else{
30+
if (n < last){
31+
return false;
32+
}
33+
}
34+
last = n;
35+
}
36+
int len = nums.size();
37+
if (s == front){
38+
return true;
39+
}
40+
return nums[len-1] <= nums[0];
41+
}
42+
};
43+
44+
45+
struct T{
46+
47+
};
48+
49+
TEST(Solution,test){
50+
T ts[] = {
51+
{
52+
53+
},
54+
55+
};
56+
57+
58+
for (T t : ts){
59+
Solution solution;
60+
61+
}
62+
}
63+
64+
int main() {
65+
testing::InitGoogleTest();
66+
67+
return RUN_ALL_TESTS();
68+
}
69+
70+

0 commit comments

Comments
 (0)