LeetCode - Algorithms - 367. Valid Perfect Square

Problem

367. Valid Perfect Square

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public boolean isPerfectSquare(int num) {
long lo = 1, hi = num;
while (lo <= hi) {
long mid = (lo + hi) / 2;
if (mid * mid == num)
return true;
if (mid * mid > num) {
hi = mid - 1;
} else
lo = mid + 1;
}
return false;
}
}

Submission Detail

  • 70 / 70 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Valid Perfect Square.
  • Memory Usage: 35.8 MB, less than 46.67% of Java online submissions for Valid Perfect Square.