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 ...
随机推荐
- C#解决并发的设计思路
解决并发的方案,应用场景,一个报名的方法,可是要限制报名的人数:一,如果是单机版,就是部署一个服务器站点的我们可以使用很经典的lock锁,或者queue队列,针对单机版二,如果是部署了集群的站点1&g ...
- 一分钟理解sdk
SDK 外语:Software Development Kit 中文:软件开发工具包 含义:一般都是一些软件工程师为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的开发工具的集合. 通俗: ...
- Android 组件化之路 资源冲突问题
比如我现在有3个模块:app模块,user模块,me模块,其中app模块依赖user模块和me模块. 然后我在user模块和me模块的strings.xml中都定义了greet字符串: // user ...
- Delphi 7的特点
- Uber回馈开源的一些软件
AresDB AresDB 是 Uber 开源的一个基于 GPU 运算的实时分析存储引擎和查询引擎.具备低查询延迟.高数据刷新率和高效内存和磁盘存储管理.AresDB 要求 CUDA Toolkit ...
- 理解BurpSuit Intruder几种攻击方式
Intruder标签下有四种攻击方式 Sniper Battering Ram Pitchfork Cluster Bomb 假设用户名密码词典分别如下: user1,user2,usre3 pass ...
- SpringBootMVC02——SpringDataJpa与ThymeLeaf
大纲 - SpringDataJpa进阶使用- SpringDataJpa自定义查询- 整合Servlet.Filter.Listener- 文件上传- Thymeleaf常用标签 1.整合Servl ...
- mysql orderby 问题
开发写的sql select * from aaa where course_id=xx order by a,b 当a,b条件都一致时,默认应该以id排序,当数据条数大于1x条(17)时,结果变为 ...
- JDBC与Hibernate的区别
相同点: ◆两者都是JAVA的数据库操作中间件. ◆两者对于数据库进行直接操作的对象都不是线程安全的,都需要及时关闭. ◆两者都可以对数据库的更新操作进行显式的事务处理. 不同点: ◆使用的SQL语言 ...
- C++中的字符数组、字符指、字符串针(腾讯)
一.字符数组 1.定义时进行初始化的方式 (1)char c[12]={'I',' ','a','m',' ','h','a','p','p','y'};//最后两个元素自动补‘\0’(不是空格),其 ...