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 ...
随机推荐
- sg函数与博弈论
这个标题是不是看起来很厉害呢... 我们首先来看一个最简单的游戏.比如我现在有一堆石子,有p个,每次可以取走若干个(不能不取),不能取的人就输了. 现在假设有两个人要玩这个游戏,一个人先手,一个人后手 ...
- Linux下Mongodb安装和启动配置
1.下载安装包 wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.2.tgz 下载完成后解压缩压缩包 tar zxf mongod ...
- PAT 1031. 查验身份证(15)
一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...
- PAT 1015. 德才论 (25)
宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子 ...
- Meet Python: little notes
Source: http://www.liaoxuefeng.com/ ❤ Escape character: '\' - '\n': newline; - '\t': tab; - '\\': \; ...
- 小tips: 使用 等空格实现最小成本中文对齐
一.重见天日第二春 11年的时候,写了篇文章“web页面相关的一些常见可用字符介绍”,这篇文章里面藏了个好东西,就是使用一些空格实现个数不等的中文对齐或等宽.见下表: 字符以及HTML实体 描述以及说 ...
- Codevs 1230 STL万岁。。 。
题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过. 输入描述 Input Description 第一行两个整数 n 和m. 第二 ...
- 用 Orchard 建立 Dynamics CRM 的入口网站
不知道国内用Orchard建网站的多不多,我个人强烈推荐这个CMS系统- 使用最新进的微软.Net 技术,而且免费!Orchard中文版在这里. 我之前写了一个基于Orchard的插件(Module) ...
- Theano2.1.15-基础知识之theano如何处理shapre信息
来自:http://deeplearning.net/software/theano/tutorial/shape_info.html How Shape Information is Handled ...
- unity3d CarWaypoints插件
编写初衷: 1.网上没有现成的好用的waypoints插件 2.自己在做一个赛车游戏,如果没有这款插件的话在制作游戏的过程中会被累成狗 3.从来没有接触过插件方面的东西,所以想自己尝试一下 插件用途: ...