LeetCode--367--有效的完全平方数
问题描述:
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
说明:不要使用任何内置的库函数,如 sqrt
。
示例 1:
输入:16
输出:True
示例 2:
输入:14
输出:False
官方:
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
left, right = 1, num
while left <= right:
mid = left + (right - left) / 2
if mid >= num / mid:
right = mid - 1
else:
left = mid + 1
return left == num / left and num % left == 0
官方2:
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
self.num=num
if num==1:
return True
low=1
high=num
while high-low>1:
mid=int((high+low)/2)
if mid**2==num:
return True
if mid**2>num:
high=mid
if mid**2<num:
low=mid
return False
违反规定:
import math
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
""" #4.0
a = str(math.sqrt(num)).split(".")[1]
if a !='':
return False
return True
另外:
import math
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
if num < 0:
return False
i = 0
while i**2 < num:
i += 1
return i**2 == num
最后为什么时间超限:
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
left = 1
right = num
while left <= num: # <= right
mid = (left + right) // 2
if mid**2 == num:
return True
if mid**2 < num:
left = mid + 1
if mid**2 > num:
right = mid -1
return False
2018-09-27 10:08:09
LeetCode--367--有效的完全平方数的更多相关文章
- Java实现 LeetCode 367 有效的完全平方数
367. 有效的完全平方数 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: ...
- LeetCode 367.有效的完全平方数(C++)
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: 输入:16 输出:True ...
- Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)
Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...
- [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) { /* 有很多 ...
- [LeetCode] 279. Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 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 ...
- [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 ...
随机推荐
- topcoder srm 679 div1
problem1 link $f[u][0],f[u][1]$表示$u$节点表示的子树去掉和不去掉节点$u$的最大权值. problem2 link 首先预处理计算任意三个蓝点组成的三角形中的蓝点个数 ...
- ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A
2018-02-19 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 mega ...
- 小C的数学问题 【单调栈】
问题 J: 小C的数学问题 时间限制: 1 Sec 内存限制: 128 MB 提交: 565 解决: 141 [提交] [状态] [命题人:外部导入] 题目描述 小C是个云南中医学院的大一新生,在 ...
- SpringBoot 使用jwt进行身份验证
这里只供参考,比较使用jwt方式进行身份验证感觉不好,最不行的就是不能退出 登陆时设定多长过期时间,只能等这个时间过了以后才算退出,服务端只能验证请求过来的token是否通过验证 Code: /** ...
- HDU 6191 Query on A Tree(可持久化Trie)
题意 \(n\) 个点的有根树,根为 \(1\) .每个点有点权,有 \(q\) 个询问,每次询问以 \(u\) 为根的子树的点的点权中异或 \(x\) 所得的最大值是多少. 思路 求出整棵树的 \( ...
- PowerDesigner使用方法入门学习
[转载:https://www.cnblogs.com/biehongli/p/6025954.html] 1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其他的概念知识可自 ...
- CPU指令集设计RISC和CISC
CPU指令集 硬件实现具有速度快,成本高,灵活性差,软件实现与之相反.因此出现频率高的基本功能首选硬件实现.指令集的不同反映了设计原理.制造技术和系统类别. RISC 精简指令集计算机(Reduced ...
- win10 右键菜单很慢的解决方式
本来想用 win7 的,不想花很多时间折腾了.现在新电脑主板硬盘CPU都在排挤 win7 ,真是可怜呀.正题: 新电脑的性能应该还算不错的, 18 年跑分 29w 以上,但在图标上面右键却都要转圈几秒 ...
- NOI1999 生日蛋糕
#include<iostream> #include<cstdio> #include<cmath> using namespace std; #define I ...
- 正则解析json数据
http://tool.chinaz.com/regex http://tool.oschina.net/regex/