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. mysql小数和类型转换函数

    保留两位小数 SELECT ROUND( 123456789.3563898,2),TRUNCATE(123456789.3563898,2),FORMAT(123456789.3563898,2); ...

  2. httpClient4.5.2工具类总结

    使用背景: 因项目使用非结构化存储,http相关jar包统一升级到httpClient4.5.2,查阅相关文档总结如下,以咨分享,望不吝指教. 依赖jar包 httpclient-4.5.2.jar. ...

  3. 在springmvc中使用requestContextListener获取全部的request对象

    RequestContextListener实现了 ServletRequestListener ,在其覆盖的requestInitialized(ServletRequestEvent reques ...

  4. GDAL联合OpenCV进行图像处理

    作为一名图像处理方面的工程师,在面对大数据量的遥感影像时,往往会利用到强大的GDAL库,但是GDAL库却没有方面的算法函数进一步进行处理:同时我们看到Opencv库能提供强大的算法支持,却对大数据影像 ...

  5. Big Data(三)伪分布式和完全分布式的搭建

    关于伪分布式的配置全程 伪分布式图示 1.安装VMWare WorkStation,直接下一步,输入激活码即可安装 2.安装Linux(需要100GB) 引导分区Boot200MB 交换分区Swap2 ...

  6. etl-p

    java excel 导入数据库 上传文件包  解压导入excel包 导入mysql

  7. 如何代替set get方法

    博主刚刚看其他人的博客的时候,发现好多人还在用 生成set get方法  虽然是自动生成的 但是看起来很复杂,影响代码的可读性 那么有什么办法能代替set  get方法吗? 当然有啦!!! 只需要导入 ...

  8. 获得 bootstrapTable行号index

    方法一: $('#table-picManager').on("click-row.bs.table",function(e,row,$element){ index=$eleme ...

  9. java课堂作业4

    第一题 字符串加密问题 1.程序设计思想 读入字符串,然后获取其长度,利用charAt()获取每个位置字符并且对字符加3实现加密处理,并存入新字符串中.如果遇到xyz则减26存入. 2.程序流程图 3 ...

  10. 《Head First 软件开发》阅读五

    结束开发循环:娟娟细流归大海 几乎完成了任务,而开发循环结束所要面对的问题是用户测试的安排.新的一轮重构和重新设计. 开发循环已经完成,但是还是有很多事情可以去做.系统测试必不可少,但是是由谁来做系统 ...