问题描述:

给定一个正整数 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--有效的完全平方数的更多相关文章

  1. Java实现 LeetCode 367 有效的完全平方数

    367. 有效的完全平方数 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: ...

  2. LeetCode 367.有效的完全平方数(C++)

    给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如  sqrt. 示例 1: 输入:16 输出:True ...

  3. Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)

    Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...

  4. [LeetCode] 367. Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  5. [leetcode]367. Valid Perfect Square验证完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  6. [LeetCode]367. Valid Perfect Square判断完全平方数

    方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...

  7. [LeetCode] 279. Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  8. [LeetCode] 0279. Perfect Squares 完全平方数

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  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 ...

  10. [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 ...

随机推荐

  1. [内核驱动] VS2012+WDK 8.0 Minifilter实现指定扩展名文件拒绝访问

    转载:http://blog.csdn.net/C0ldstudy/article/details/51585708 转载:http://blog.csdn.net/zj510/article/det ...

  2. Shell脚本,更改Info.plist中的日期等

    #!/bin/bashroot_src=$(dirname $(PWD)) bundle_name='RandomDebbot.bundle' target_path=$root_src/ecovac ...

  3. 用uniGUI做B/S下业务系统的产品原型体验

    从10月份到重庆工作后,一直忙于工作,感兴趣的几个方面的技术都处于暂停. 一个多月来,按照公司要求在做B/S集中式基卫产品的原型,主要是画原型图,开始是用Axure,弄来弄去感觉功能还是弱了些,尤其是 ...

  4. Vue学习【第一篇】:Vue初识与指令

    什么是Vue 什么是Vue Vue.js是一个渐进式JavaScript框架它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 渐进式:vue从小到控制页面中的一个变量 ...

  5. LOJ#2452. 「POI2010」反对称 Antisymmetry

    题目描述 对于一个 \(0/1\) 字符串,如果将这个字符串 \(0\) 和 \(1\) 取反后,再将整个串反过来和原串一样,就称作「反对称」字符串.比如 \(00001111\) 和 \(01010 ...

  6. Winform选择目录路径与选择文件路径

    https://blog.csdn.net/zaocha321/article/details/52528279 using System.Collections.Generic; using Sys ...

  7. Matconvnet 的一些记录

    Matconvnet 的一些记录 Example code from ADNet: Action-Decision Networks for Visual Tracking with Deep Rei ...

  8. Vue学习一:{{}}html模板使用方法

    本文为博主原创,未经允许不得转载: 之前自学了vue,在项目中应用了vue,由于是第一次使用,感觉非常强大,使用也非常方便,趁有时间,总结一下vue学习过程中 各个指令的使用方法,只要掌握了vue的指 ...

  9. Ubuntu 18.04版本下安装网易云音乐

    这是我迄今为止发现的最完美的解决方法,不用改任何东西,只需要安装然后打开即可,后台也有. 参考:http://archive.ubuntukylin.com:10006/ubuntukylin/poo ...

  10. Lintcode452-Remove Linked List Elements-Easy

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...