367. Valid Perfect Square
Easy
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt
.
Example 1:
Input: 16
Output: true
Example 2:
Input: 14
Output: false
package leetcode.easy; public class ValidPerfectSquare { public boolean isPerfectSquare(int num) { for (long i = 1; i <= num; i++) { if (i * i == num) { return true; } else if (i * i > num) { break; } else { continue; } } return false; } @org.junit.Test public void test() { System.out.println(isPerfectSquare(16)); System.out.println(isPerfectSquare(14)); } }