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 ...
随机推荐
- Python类函数调用:missing 1 required positional argument
在Python中,应该先对类进行实例化,然后在应用类.注意,实例化的过程是应该加括号的.
- mysql元数据以及一些常用命令
所谓mysql元数据就是一些初始的东西,例如数据库的列表,数据表列表,查询影响的行数等等,还有就是mysql的服务器的一些信息,例如版本信息等. select version(): 获取mysql服务 ...
- 利用wampserve搭建本服务器
1.官网下载安装包 注意:3.0.6版本需要下载依赖包vc依赖包 2.默认为英文 右击图标进入langue设置为中文 3.需要手动设置在现状态 右击=>选中wampsetting =>me ...
- jumpserver模块功能介绍
一.仪表盘二.用户管理1.用户列表2.用户组 三.资产管理 1.资产列表 1.1 管理资产树 资产树节点不能重名, 右击节点可以添加.删除和重命名节点, 以及进行资产相关的操作 1.2 为资产树节点创 ...
- javase中的super、this和protected关键字
关键字 this 记住一句话:this是指向对象本身的一个指针. 注意: this只能用于方法体内,且只能在非静态方法中,也就是说只有实例调用的方法中才能出现this,我们可以认为当new了一个实例后 ...
- hive模拟数据
人员表 id,姓名,爱好,住址 1,小明1,lol-book-movie,beijing:mashibing-shanghai:pudong 2,小明2,lol-book-movie,beijing: ...
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
- [工具] BurpSuite--Intruder功能
BurpSuite--Intruder功能 0x00 配置说明 intruder是进行爆破的,基本流程是标注请求的爆破参数,然后配置字段,选择爆破方式进行爆破,下面来记录下工具的使用 选中intrud ...
- scrapy 知乎关键字爬虫spider代码
以下是spider部分的代码.爬知乎是需要登录的,建议使用cookie就可以了,如果需要爬的数量预计不多,请不要使用过大的线程数量,否则会过快的被封杀,需要等十几个小时账号才能重新使用,比起损失的这十 ...
- java高并发核心要点|系列5|CPU内存伪共享
上节提到的:伪共享,今天我们来说说. 那什么是伪共享呢? 这得从CPU的缓存结构说起.以下如图,CPU一般来说是有三级缓存,1 级,2级,3级,越上面的,越靠近CPU的,速度越快,成本也越高.也就是说 ...