题目描述:

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

解题分析:

这种找数字的题一般都用类似与二分查找的算法。需要注意的是比较平方和时考虑到integer溢出的情况。所以这个结果是要用Long类型保存。由此到来的改变是判断相等时要用“equals()”方法,而不是“==”。

实现代码:

 public class Solution {
public static boolean isPerfectSquare(int num) {
if(num==1||num==4)
return true;
if(num<=0||num==2||num==3)
return false;
int from=0;
int to=num;
while(from<=to){
int mid = (from+to)/2;
Long result = (long)mid * (long)mid;
if(result.equals(Long.valueOf(num+"")))
return true;
if(result<num){
from=mid+1;
}
if(result>num){
to=mid-1;
} }
return false;
}
}

 

【leetcode】367. Valid Perfect Square的更多相关文章

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

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

  2. 【easy】367. Valid Perfect Square 判断是不是平方数

    class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...

  3. 【LeetCode】422. Valid Word Square 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...

  4. Python 解LeetCode:367. Valid Perfect Square

    题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方. 思路:直接使用二分法,貌似没啥好说的.代码如下: class Solution(object): def is ...

  5. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 367. Valid Perfect Square

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

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

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

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

随机推荐

  1. apache2.2 虚拟主机配置

    一.改动httpd.conf 打开appserv的安装文件夹,找到httpd.conf文件,分别去掉以下两行文字前面的#号. #LoadModule vhost_alias_module module ...

  2. 简单的div元素拖拽到div

    drag1 drag2 drag3 代码如下: <!DOCTYPE HTML> <html> <head> <title>div拖拽到div</t ...

  3. 索引节点inode

    在Linux的文件系统中,索引节点是文件的标识,并且这个值是唯一的,两个不同的文件的索引节点值是不同的,索引节点相同的文件它们的内容是相同的,仅仅文件名不同.修改两个索引节点值相同的文件中的一个文件, ...

  4. c#给用户控件添加事件处理程序

    1.首先在usercontrol后台添加如下代码: public partial class MyControl: UserControl { //添加事件代理       public event ...

  5. C#实现万年历(农历、节气、节日、星座、属相、生肖、闰年等)

    C# 万年历 农历 节气 节日 星座 星宿 属相 生肖 闰年月 时辰等,代码如下: using System.Collections.Generic; using System.Text; using ...

  6. 1.7.7 Spell Checking -拼写检查

    1. SpellCheck SpellCheck组件设计的目的是基于其他,相似,terms来提供内联查询建议.这些建议的依据可以是solr字段中的terms,外部可以创建文本文件, 或者其实lucen ...

  7. apache2.4的安装与卸载

    安装sudo apt-get install apache2,这不是源码安装的方式,产生的apache地址在/etc/apache2,配置文件是apache2.conf如果浏览器输入127.0.0.1 ...

  8. Divisibility by Eight (数学)

    Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. hdu 4494 最小费用流

    思路:这题我在下午重现的时候就用的费用流做,可是各种悲催的超时,只是我一开始的那种建图方式多了一个二分查找. 戏剧性的是,求距离的返回值写成int型了,CodeBlock编译器又没有警告,然后就WA啊 ...

  10. Linux 字符设备控制技术

    字符设备控制技术 一.字符设备控制理论 1.作用 大部分设备除了具有<读写设备>的能力,还应该有<控制设备>的能力.比如改变 波特率 2. 应用程序接口 在用户空间中使用 Io ...