Python 解LeetCode:367. Valid Perfect Square
题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方。
思路:直接使用二分法,貌似没啥好说的。代码如下:
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
left, right = 0, num
while left <= right:
mid = (left+right) / 2
if mid ** 2 == num:
return True
elif mid ** 2 < num:
left = mid + 1
else:
right = mid -1
return False
Python 解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
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 ...
随机推荐
- mvc的filter
如果想要记录ajax的请求和输出信息.内部发生异常记录日志.需要登录认证.需要权限判断:那mvc的各种filter可以帮助你实现你想要的.Mvc框架支持5种不同类型的过滤器:我会按照执行顺序进行简单的 ...
- python常见模块命令(os/sys/platform)
一.Os Python的标准库中的os模块主要涉及普遍的操作系统功能.可以在Linux和Windows下运行,与平台无关. os.sep 可以取代操作系统特定的路径分割符. os.name字符串指示你 ...
- GBK和UTF8有什么区别
GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字符. UTF-8编码:它是一种全国家通过的一种编码,如果你的网站涉及到多 ...
- MySQL主从复制 + Mycat实现读写分离
说明:两台MySQL服务器都是使用CentOS6.5系统,MySQL版本为mysql-5.7.17 MySQL一主一被实现主从复制 注意:写包括insert,delete,update 操作:读只有s ...
- 使用OLAMISDK实现一个语音输入数字进行24点计算的iOS程序
前言 在目前的软件应用中,输入方式还是以文字输入方式为主,但是语音输入的方式目前应用的越来越广泛.这是一个利用 Olami SDK 编写的一个24点iOS程序,是通过语音进行输入. Olami SDK ...
- masonry 设置控件抗压缩及抗拉伸
使用masonry正常设置约束时两个label的显示是下图 添加代码设置蓝色label的抗压缩属性后( [self.missionNameLabel setContentCompressionResi ...
- 用Python解答百度测试开发算法面试题
吾八哥本人之前有幸能接到百度北京总部的人工智能测试开发岗位的面试机会,在二面的过程中,面试官出了一道算法题,题目是:有一组"+"和"-"符号,要求将" ...
- 起名字好难啊!(初识Django)
这次我们将实现一个简单的登录注册功能,并吧相应的数据写入数据库: 做这件事之前我已经在数据库中新建了两张表(当然一张表也可以用,先注册后登录嘛···) 两张结构很简单的数据表:↓ 接下来就该干正 ...
- Javascript常见浏览器兼容问题
常见浏览器原生javascript兼容性问题主要分为以下几类: 一.Dom 1.获取HTML元素,兼容所有浏览器方法:document.getElementById("id")以I ...
- Centos7安装后出现please make your choice from '1' to e 解决方式
[输入"1",按Enter键 输入"2",按Enter键 输入"q",按Enter键 输入"yes",按 ...