> For the complete documentation index, see [llms.txt](https://rachel2011.gitbook.io/leetcode_cc150/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rachel2011.gitbook.io/leetcode_cc150/binary-search/valid-perfect-square.md).

# 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:**

```
Input: 16
Returns: True
```

**Example 2:**

```
Input: 14
Returns: False
```

**Binary Search**

```
class Solution(object):
    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """
        left = 0
        right = num
        while left <= right:
            mid = (left + right)/2
            if mid*mid == num:
                return True
            elif mid*mid < num:
                left = mid +1
            else:
                right = mid -1
        return False
```

其他解法：<http://www.cnblogs.com/grandyang/p/5619296.html>

## 二刷：

```
class Solution(object):
    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """
        start, end = 1, num
        while start +1 < end:
            mid = (start+end)/2
            if mid*mid < num:
                start = mid
            else:
                end = mid
        if start*start == num or end*end == num:
            return True
        return False
```
