[leetcode:python]69.Sqrt(x)
来源:互联网 发布:如何建设企业文化 知乎 编辑:程序博客网 时间:2024/06/05 14:56
题目:求平方根
Implement int sqrt(int x).
Compute and return the square root of x.
方法一:性能62ms
class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ #二分查找 if x < 2: return x low, high = 1, x/2 last = 0 while low <= high: mid = (low + high)/2 if mid * mid > x: high = mid -1 elif mid * mid < x: low = mid + 1 last = mid else: return mid return last
方法二:性能45ms
class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ left = 0 right = x while(left <= right): mid = (left + right)/2 if(mid*mid < x): left = mid + 1 elif(mid*mid > x): right = mid -1 else: return mid return left - 1
感觉两个方法一样。。
0 0
- [leetcode:python]69.Sqrt(x)
- 【Leetcode】【python】Sqrt(x)
- LeetCode--Sqrt(x)Python
- [Leetcode]69. Sqrt(x)解析@Python
- 【leetcode】69. Sqrt(x)(Python & C++)
- LeetCode 69. Sqrt(x)
- LeetCode 69.Sqrt(x)
- LeetCode --- 69. Sqrt(x)
- [Leetcode] 69. Sqrt(x)
- [leetcode] 69.Sqrt(x)
- Leetcode-69.Sqrt(x)
- leetcode--69.sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- 69. Sqrt(x) LeetCode
- [leetcode] 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- 再谈布局之 UIStackView
- Android开发中获取当前网络类型的方法
- Android 性能优化——避免过度绘制
- Android6.0运行时权限管理
- React Native环境配置之Windows版本搭建
- [leetcode:python]69.Sqrt(x)
- google 浏览器使用的一些小技巧
- 多线程下载
- 2017年5月校赛赛前练习 最大连续区间和
- C++ string(初始化和部分函数的使用)
- Android 自定义控件之继承ViewGroup创建新容器
- Android编译不成功的原因
- [模块]()
- java.lang.IllegalArgumentException: No converter found for return value of type:class 解决方案