367. valid perfect square

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:

1
2
Input: 16
Output: true

Example 2:

1
2
Input: 14
Output: false
1
2
3
4
5
6
7
8
class  {
public boolean isPerfectSquare(int num) {
for (int i = 1; num > 0; i += 2) {
num -= i;
}
return 0 == num;
}
}