lurenaa的博客

😂二分查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
bool isPerfectSquare(int num) {
int lo = 0,
ho = num / 2 + 1, mid;
while(lo < ho) {
mid = lo + (ho - lo) / 2;
if(pow(mid, 2) < num) {
lo = mid + 1;
} else
ho = mid;
}
if(pow(lo , 2) == num)
return true;
return false;
}
};

Accepted

68/68 cases passed (0 ms)

Your runtime beats 100 % of cpp submissions

Your memory usage beats 54.55 % of cpp submissions (8 MB)