leetcode-mid-math - 69. Sqrt(x)-NO
mycode memory error
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
if x == 0 : return 0
if x == 1 or x==2 or x ==3 : return 1 for i in range(2,x//2):
if i**2 > x :
return i-1
参考:
1、但是下面这个可以过!!!难道range会存储???????????
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
if x == 0 :return 0
k = 1
res = 1
while (k+1)*(k+1) <= x:
k += 1
return k
2、
class Solution(object):
def mySqrt(self, x):
return int(x**0.5)
3、 我也试了类似二分的方法想缩小范围,但是没有成功。。。
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
""" if x == 1 or x==0:
return x begin , last = 0, x
#当x>1时, 0*0<x,x*x>x
while begin < last:
mid = (begin + last)//2 if mid**2 == x:
return mid
elif mid**2 > x :
last = mid elif mid**2 < x:
if (mid+1)**2 > x:
return mid
else:
begin = mid return -1
leetcode-mid-math - 69. Sqrt(x)-NO的更多相关文章
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- 69. Sqrt(x) - LeetCode
Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...
- LeetCode 69. Sqrt(x) (平方根)
Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...
- [LeetCode] 69. Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- 【LeetCode】69. Sqrt(x) 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:库函数 方法二:牛顿法 方法三:二分查找 日 ...
- 【一天一道LeetCode】#69. Sqrt(x)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
- (二分查找 拓展) leetcode 69. Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
随机推荐
- RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较
原文:RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较 分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 这是网上的一篇教程写的很好,不知原作 ...
- luogu P5328 [ZJOI2019]浙江省选
传送门 每个人都可以看成一条直线\(y=ax+b\),所以我们要求的是每条线在整点处,上方线的数量的最小值(注意多条直线如果交于同一整点互不影响) 如果\(m=1\),其实只要求出半平面交,然后在半平 ...
- DNS域名配置
1.什么是DNS 2.DNS层次介绍及基础内容 3.DNS的工作原理及过程 域名服务器配置实战: 4.主域名服务器配置 5.辅域名服务器配置 6.缓存域名服务器配置 1.什么是D ...
- mybatis一对一关联关系映射
mybatis一对一关联关系映射 在关联关系中,有一对一,一对多,多对多三种关联关系. 一对一关系:在操作上,任意一方引入对方的主键作为外键. 一对多关系:在"多"的一方添加&qu ...
- hbase报错org.apache.hadoop.ipc.RemoteException(java.lang.NullPointerException): java.lang.NullPointerException
这个错误找了好长时间,日志看半天,找度娘没什么用,再看stackoverflow,上面有几篇相关的帖子,但是没人回答. 看半天,没有cause,只好到idea中去看sortLocalSorts源码,结 ...
- git 版本回退方法
ORIG_HEAD 某些操作,例如 merage / reset 会把 merge 之前的 HEAD 保存到 ORIG_HEAD 中,以便在 merge 之后可以使用 ORIG_HEAD 来回滚到合并 ...
- mac StarUML3.0.2破解
mac StarUML3.0.2破解 StarUML是一款开放源码的UML开发工具,是由韩国公司主导开发出来的产品,可以直接到StarUML网站下载.可以用来创建UML类图. 破解办法是通过asar工 ...
- java Class类的用法示例
@SuppressWarnings("unchecked") public void func() throws InstantiationException, IllegalAc ...
- centos7进入单用户模式修改root密码
1.开机 按“e”,然后输入init=/bin/sh 2.根据提示按ctrl+x 得如下图: 3.输入mount -o remount,rw / 输入passwd设置新密码.如下图: 4.输 ...
- zabbix修改默认字体
在zabbixweb界面里有时候显示界面为框框 所以修改默认字体 现在windows里 随便找个字体: win+R 搜fonts 然后随便复制了一个字体,传到zabbix服务器里 cp calib ...