Leetcode 367. Valid Perfect Square
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
本题可以用binary search。
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
low, high = 1, num
while low <= high:
mid = (high + low)//2
if mid**2 == num:
return True
elif mid**2 > num:
high = mid - 1
else:
low = mid + 1
return False
Leetcode 367. Valid Perfect Square的更多相关文章
- [LeetCode] 367. Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode]367. Valid Perfect Square判断完全平方数
方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- 【leetcode】367. Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- [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 ...
- 367. Valid Perfect Square判断是不是完全平方数
[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- [LC] 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- SPM FDR校正
来源: http://blog.sciencenet.cn/blog-479412-572049.html,http://52brain.com/thread-15512-1-1.html SPM8允 ...
- Java 集合系列12之 TreeMap详细介绍(源码解析)和使用示例
概要 这一章,我们对TreeMap进行学习.我们先对TreeMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用TreeMap.内容包括:第1部分 TreeMap介绍第2部分 TreeMa ...
- codevs2806 红与黑
难度等级:白银 codevs2806 红与黑 题目描述 Description 有一个矩形房间,覆盖正方形瓷砖.每块瓷砖涂成了红色或黑色.一名男子站在黑色的瓷砖上,由此出发,可以移到四个相邻瓷砖之一, ...
- TF400916错误修复办法
在使用TFS作为研发过程管理工具的时候,如果调整了工作项的状态信息,可能会出现下面的错误: 要解决此问题非常简单: 1.找一台安装了VS2015程序的环境.因为我们使用的是TFS2015,所以需要对应 ...
- 移动端页面(css)调试之“weinre大法”
移动端页面调试一般分两步.第一步我们需要把本地(pc端)写的页面效果展现在移动端,一个很方便的办法是用 fiddler 作为代理,具体可以参考 如何用 fiddler 代理调试本地手机页面,这样我们就 ...
- java并发:简单面试问题集锦
多线程:Simultaneous Multithreading,简称SMT. 并行.并发 并行性(parallelism)指两个或两个以上的事件在同一时刻发生,在多道程序环境下,并行性使多个程序同一时 ...
- 使用windbg查看DependencyObject的属性
这里以WPF作为探测用的例子,简单一些,看看Title的值是什么样子.(之所以写这个,因为不是简单的一个!do就能看到东西的,中间要绕两下,这也涉及到了DependencyObject的实现机制及数据 ...
- SQL Server使用游标或临时表遍历数据
方法一:使用游标(此方法适用所有情况,对标结构没有特殊要求.) declare @ProductName nvarchar() declare pcurr cursor for select Prod ...
- 【BZOJ1002】【FJOI2007】轮状病毒(生成树计数)
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1766 Solved: 946[Submit][Status ...
- 【JavaEE企业应用实战学习记录】MyGetAttributeListener
package sanglp.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletContextAttr ...