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的更多相关文章

  1. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  2. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  3. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

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

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

  6. 【LeetCode】69. Sqrt(x) 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:库函数 方法二:牛顿法 方法三:二分查找 日 ...

  7. 【一天一道LeetCode】#69. Sqrt(x)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

  8. Leetcode 69. Sqrt(x)

    Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...

  9. (二分查找 拓展) 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 ...

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

随机推荐

  1. 运维LVS-NAT模式理解

    一.LVS-NAT模式的工作原理这个是通过网络地址转换的方法来实现调度的.首先调度器(LB)接收到客户的请求数据包时(请求的目的IP为VIP),根据调度算法决定将请求发送给哪个 后端的真实服务器(RS ...

  2. vue.js(5)--事件修饰符

    vue中的事件修饰符(.stop..prevent..self..capture..once) (1)实例代码 <!DOCTYPE html> <html lang="en ...

  3. 【vue】canvas验证码组件--数字/数字加字母

    基于canvas的数字/数字+字符验证码   SIdentify.vue 组件 <!-- 基于canvas的数字/数字+字符验证码 --> <!-- 调用格式 <s-ident ...

  4. MySQL主备同步延迟

    今天看到mycat的日志,发现在wrapper.log中频繁warning:slave延迟23006秒 查看数据指标: show engine innodb status正常,而且slave的拷贝点在 ...

  5. wpf Textbox 回车就换行

    将 TextWrapping 属性设置为 Wrap 会导致输入的文本在到达 TextBox 控件的边缘时换至新行,必要时会自动扩展 TextBox 控件以便为新行留出空间. 将 AcceptsRetu ...

  6. 怎样减少 Android 应用包 60% 的大小?

    简评: 应用的大小也是用户体验的一个重要方面,而减少 Android 应用安装包大小其实一点也不复杂. 对于移动应用来说,应用安装包的大小当然是越小越好.特别是对于一些欠发达地区,你不希望用户因为手机 ...

  7. Jsoup抓取网页数据完成一个简易的Android新闻APP

    前言:作为一个篮球迷,每天必刷NBA新闻.用了那么多新闻APP,就想自己能不能也做个简易的新闻APP.于是便使用Jsoup抓取了虎扑NBA新闻的数据,完成了一个简易的新闻APP.虽然没什么技术含量,但 ...

  8. DataWorks参数配置

    https://help.aliyun.com/document_detail/30281.html?spm=a2c6h.13066369.0.0.7bef69daI5ajKt

  9. react在视频中截图,保存为base64位

    wq:之前看了网上很多教程,有点模糊,但是最后还是搞了出来 1  不要将视频放到canvas上面!  之前一直将video重新画到canvas上面,然后再次将第一个canvas放到第二个canvas上 ...

  10. react保存用户的输入换行,空格等等

    <div dangerouslySetInnerHTML = {{ __html: '接口返回值' }} />