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

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

这道题给了我们一个数,让我们判断其是否为完全平方数,那么显而易见的是,肯定不能使用 brute force,这样太不高效了,那么最小是能以指数的速度来缩小范围,那么我最先想出的方法是这样的,比如一个数字 49,我们先对其除以2,得到 24,发现 24 的平方大于 49,那么再对 24 除以2,得到 12,发现 12 的平方还是大于 49,再对 12 除以2,得到6,发现6的平方小于 49,于是遍历6到 12 中的所有数,看有没有平方等于 49 的,有就返回 true,没有就返回 false,参见代码如下:

解法一:

class Solution {
public:
bool isPerfectSquare(int num) {
if (num == ) return true;
long x = num / , t = x * x;
while (t > num) {
x /= ;
t = x * x;
}
for (int i = x; i <= * x; ++i) {
if (i * i == num) return true;
}
return false;
}
};

下面这种方法也比较高效,从1搜索到 sqrt(num),看有没有平方正好等于 num 的数:

解法二:

class Solution {
public:
bool isPerfectSquare(int num) {
for (int i = ; i <= num / i; ++i) {
if (i * i == num) return true;
}
return false;
}
};

我们也可以使用二分查找法来做,要查找的数为 mid*mid,参见代码如下:

解法三:

class Solution {
public:
bool isPerfectSquare(int num) {
long left = , right = num;
while (left <= right) {
long mid = left + (right - left) / , t = mid * mid;
if (t == num) return true;
if (t < num) left = mid + ;
else right = mid - ;
}
return false;
}
};

下面这种方法就是纯数学解法了,利用到了这样一条性质,完全平方数是一系列奇数之和,例如:

1 = 1
4 = 1 + 3
9 = 1 + 3 + 5
16 = 1 + 3 + 5 + 7
25 = 1 + 3 + 5 + 7 + 9
36 = 1 + 3 + 5 + 7 + 9 + 11
....
1+3+...+(2n-1) = (2n-1 + 1)n/2 = n*n

这里就不做证明了,我也不会证明,知道了这条性质,就可以利用其来解题了,时间复杂度为 O(sqrt(n))。

解法四:

class Solution {
public:
bool isPerfectSquare(int num) {
int i = ;
while (num > ) {
num -= i;
i += ;
}
return num == ;
}
};

下面这种方法是第一种方法的类似方法,更加精简了,时间复杂度为 O(lgn):

解法五:

class Solution {
public:
bool isPerfectSquare(int num) {
long x = num;
while (x * x > num) {
x = (x + num / x) / ;
}
return x * x == num;
}
};

这道题其实还有 O(1) 的解法,这你敢信?简直太丧心病狂了,详情请参见论坛上的这个帖子

Github 同步地址:

https://github.com/grandyang/leetcode/issues/367

类似题目:

Sqrt(x)

参考资料:

https://leetcode.com/problems/valid-perfect-square/

https://leetcode.com/problems/valid-perfect-square/discuss/83872/O(1)-time-c%2B%2B-solution-inspired-by-Q_rsqrt

https://leetcode.com/problems/valid-perfect-square/discuss/83874/A-square-number-is-1%2B3%2B5%2B7%2B...-JAVA-code

https://leetcode.com/problems/valid-perfect-square/discuss/83902/Java-Three-Solutions-135..-SequenceBinary-SearchNewton

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 367. Valid Perfect Square 检验完全平方数的更多相关文章

  1. [leetcode]367. Valid Perfect Square验证完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  2. [LeetCode]367. Valid Perfect Square判断完全平方数

    方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...

  3. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  4. Leetcode 367. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  5. 367. Valid Perfect Square

    原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...

  6. 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...

  7. 【leetcode】367. Valid Perfect Square

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  8. 367. Valid Perfect Square判断是不是完全平方数

    [抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  9. [LeetCode] 367. Valid Perfect Square_Easy tag:Math

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

随机推荐

  1. 行为驱动:Cucumber + Java - 实现数据的参数化

    1.什么是参数化 实际设计测试用例过程中,我们经常会用等价类.边界值这样的方法,针对一个功能进行测试数据上的测试,比如一个输入框,正向数据.逆向数据,非法输入等等 2.Cucumber的数据驱动 同上 ...

  2. 简述vue props和非props的2个特性

    props的2个特性 ①:父组件通过属性的方式传值(比如下面截图中的content)给子组件,content不会显示在DOM节点中                       ②:父组件向子组件传递值 ...

  3. 转:对softmax讲解比较清楚的博客

    https://blog.csdn.net/wgj99991111/article/details/83586508

  4. python 多进程和多线程对比

    1. 对于耗费CPU的操作来说,多进程优于多线程 2. 对于耗费IO操作来说,多线程优于多进程 3. 多进程切换代价大于多线程

  5. springboot 使用 freemarker 无法正常跳转的问题?

    1.springboot 使用 freemarker 无法正常跳转的问题? 参考:https://blog.csdn.net/Lin_xiaofeng/article/details/79122053 ...

  6. SpringBoot与Swagger整合

    1 SpringBoot与Swagger整合https://blog.csdn.net/jamieblue1/article/details/99847744 2 Swagger详解(SpringBo ...

  7. Python - 标准库概况 - 第二十一天

    Python 标准库概览 操作系统接口 os模块提供了不少与操作系统相关联的函数. 建议使用 "import os" 风格而非 "from os import *&quo ...

  8. JQuery 遍历table中的checkbox 并对行数据进行校验

    JQuery中confirm的使用 $(document).ready(function () { $("#Btn_Print").click(function () { var ...

  9. Python中的常见特殊方法—— del方法

    __del__() 方法用于销毁Python对象——在任何Python对象将被系统回收的时候,系统都会自动调用这个方法.但是不要以为对一个变量执行del操作,该变量引用的对象就会被回收,当然不是,如果 ...

  10. maven工程运行前准备

    近一年来,我学习了很多java及数据库前沿技术,如Spring框架,SpringMVC框架,Mybatis框架,Redis,Dubbo,Maven等. 以及一些Linux命令和在Linux环境下的工程 ...