题目描述:

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. 发现js端日期跟php端日期格式不一致

    当程序没有问题,而php显示的日期和js显示的日期不一致,相差一天的时候或者其它,一定要看看php.ini中时区的配置是否合理.

  2. $parse/$eval和$observe/$watch如何区分

    大家在看angular的时候,有时候偶尔会看到$parse,$eval和$observe,$watch这两对语法,随着深入使用angular,就不可避免使用到它.文章从内部运行机制跟实际需求的角度来解 ...

  3. MySQL中部分系统变量介绍

      have_symlink                             DISABLED                                   YES 用以支持在表定义中指 ...

  4. SQL Server 查看死锁的存储过程(转载)

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]') and ) drop proc ...

  5. Python Learning

    这是自己之前整理的学习Python的资料,分享出来,希望能给别人一点帮助. Learning Plan Python是什么?- 对Python有基本的认识 版本区别 下载 安装 IDE 文件构造 Py ...

  6. spark1.2.0安装

    standalone 安装SCALA 下载.解压.加入环境变量 安装spark1.2.0 下载.解压.加入环境变量 tar zxvf spark--bin-.tgz export SPARK_HOME ...

  7. iOS H5容器的一些探究(二):iOS 下的黑魔法 NSURLProtocol

    来源:景铭巴巴 链接:http://www.jianshu.com/p/03ddcfe5ebd7 iOS H5 容器的一些探究(一):UIWebView 和 WKWebView 的比较和选择 一.前言 ...

  8. Solr 删除数据的几种方式

    原文出处:http://blog.chenlb.com/2010/03/solr-delete-data.html 有时候需要删除 Solr 中的数据(特别是不重做索引的系统中,在重做索引期间).删除 ...

  9. 【阿里云产品公测】ACE、BAE及SAE云引擎对比评测

    作者:阿里云用户bailimei 先前我已发布一篇关于ACE的评测,在跟朋友聊天的时候我们讨论了目前市面上的三款云引擎产品,这三家的云引擎我都有在用,今天有时间顺便写篇关于阿里云ACE.百度BAE和新 ...

  10. ASCII码表 char(9),char(10),char(13)等

    char(9)   水平制表符 char(10) 换行 char(13) 回车 测试ASCII码的方法: 在记事本中,按住ALT键,同时用小键盘输入十进制的ASCII码,然后松手,就可以看到效果了! ...