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] 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验证完全平方数

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

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

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

  4. LeetCode "Valid Perfect Square"

    Typical binary search.. but take care of data overflow if you are using C++ class Solution { public: ...

  5. Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)

    Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...

  6. 367. Valid Perfect Square

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

  7. LeetCode_367. Valid Perfect Square

    367. Valid Perfect Square Easy Given a positive integer num, write a function which returns True if  ...

  8. C#LeetCode刷题之#367-有效的完全平方数(Valid Perfect Square)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3869 访问. 给定一个正整数 num,编写一个函数,如果 num ...

  9. [Swift]LeetCode367. 有效的完全平方数 | Valid Perfect Square

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

随机推荐

  1. nodejs、npm、grunt——名词解释

    最近着手开发一个新项目,打算从工程化的角度整理一套自己的前端开发.发布体系. grunt这些工具,之前别人用我也用,并没有认真想过它们的前世今生,正好趁着这个机会,我来理一理目前业界比较流行这些工具的 ...

  2. Cesium原理篇:GroundPrimitive

    今天来看看GroundPrimitive,选择GroundPrimitive有三个目的:1 了解GroundPrimitive和Primitive的区别和关系 2 createGeometry的特殊处 ...

  3. .net汉字转字母

    目前手上有一个需要实现:将用户输入的姓名转换成汉语拼音. 使用枚举,既麻烦又易出错,发现有一个微软拼音转换工具类ChnCharInfo.dll,在此记录下: 首先需要引入此dll, 链接: http: ...

  4. Java进击C#——语法之线程同步

    上一章我们讲到关于C#线程方向的应用.但是笔者并没有讲到多线程中的另一个知识点--同步.多线程的应用开发都有可能发生脏数据.同步的功能或多或少都会用到.本章就要来讲一下关于线程同步的问题.根据笔者这几 ...

  5. Ionic2系列——在Ionic2中使用ECharts

    在群里看到有人问怎么在Ionic2中集成ECharts来显示图表.当时答应说写个blog,简单写下步骤. 在TypeScript中如果要使用第三方库,必须要有d.ts,也就是定义文件,没有这个文件的话 ...

  6. redis安装记录

    下载redishttps://redis.io/    下载 3.2.6版本 ,上传到服务器 . 解压tar -zxvf redis-3.2.6 .tar.gz 修改配置文件(修改redis.conf ...

  7. jQuery版AJAX简易封装

    开发过程中,AJAX的应用应该说非常频繁,当然,jQuery的AJAX函数已经非常好用,但是小编还是稍微整理下,方便不同需求下,可以简化输入参数,下面是实例代码: $(function(){ /** ...

  8. HTML常用标签

    HTML常用标签: HTML文档格式: 首先,HTML是一种超文本标签语言,它是制作网页的基础. 其次,HTML文档中至少包含基本的和成对的<html> </html>.< ...

  9. shift粘滞键后门创建/复原批处理

    创建shift粘滞键后门: 1 c: 2 3 cd \Windows\System32\ 4 5 rename sethc.exe bak_sethc.exe 6 7 xcopy cmd.exe se ...

  10. Atitit ati licenseService    设计原理

    Atitit ati licenseService    设计原理 C:\0workspace\AtiPlatf\src_atibrow\com\attilax\license\LicenseX.ja ...