-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path367.valid-perfect-square.cpp
More file actions
48 lines (47 loc) · 975 Bytes
/
367.valid-perfect-square.cpp
File metadata and controls
48 lines (47 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @lc app=leetcode id=367 lang=cpp
*
* [367] Valid Perfect Square
*/
// @lc code=start
class Solution
{
public:
int MAX = 1 << 16;
bool isPerfectSquare(int num)
{
long long _num = num;
long long left = 1;
long long right = _num;
long long mid;
long long res;
while (1)
{
if (left > right)
return false;
mid = (left + right) >> 1;
if (mid > MAX)
{
right = mid - 1;
continue;
}
long long res = (long long)mid * mid;
if (res > _num)
{
right = mid - 1;
continue;
}
else if (res < _num)
{
left = mid + 1;
continue;
}
else
{
return true;
}
}
return false;
}
};
// @lc code=end