367 Valid Perfect Square
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
Given a positive integernum, write a function which returns True ifnumis a perfect square else False.
Note:Do not use any built-in library function such assqrt
.
Example 1:
Input: 16
Returns: True
Example 2:
Input: 14
Returns: False
(1) Binary Search
class Solution {
public boolean isPerfectSquare(int num) {
if (num < 1) {
return false;
}
long start =
Binary Search: 时间复杂度O(logn), 空间复杂度O(1)