leetcode ValidPerfectSquare
z
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
public class ValidPerfectSquare {
/*
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
*/
public boolean isPerfectSquare(int num) {
// 时间代价太高,可以使用二分搜索
int i=num/2;
if (num < 0){
return false;
}
for (int n = 0; n<=i; n++){
if (n*n == num)
return true;
}
return false;

}
}