# 367.Valid Perfect Square

**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
```

不能用sqrt函数，那么就只能直接循环，好在也不需要求出平方根也可以知道大致在什么位置跳出循环。

```
class Solution {
public:
    bool isPerfectSquare(int num) {
        for(int i=1;i<=num;i++)
        {
            long  tmp=i*(long)i;
           if(tmp>num) return false;
            else if (tmp == num ) return true;
        }
        return false;
    }
};
```

这里将i转成long防止整型溢出。当tmp大于num时，即i已经超过了平方根，直接返回false即可。

> 执行用时 :4 ms, 在所有 C++ 提交中击败了81.42%的用户\
> 内存消耗 :8 MB, 在所有 C++ 提交中击败了61.50%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/367.valid_perfect_square.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
