lurenaa的博客

😂二分查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
int lo = 0 , hi = letters.size(), m;
while(hi > lo) {
m = lo + (hi - lo) / 2;
if(letters[m] <= target) {
lo = m + 1;
} else
hi = m;
}
if(lo >= letters.size())
return letters[0];
return letters[lo];
}
};

Accepted

165/165 cases passed (4 ms)

Your runtime beats 99.88 % of cpp submissions

Your memory usage beats 11.87 % of cpp submissions (9.1 MB)