leetcode69
public class Solution {
public int MySqrt(int x) {
long r = x;
while (r * r > x)
r = (r + x / r) / ;
return (int)r;
}
}
https://leetcode.com/problems/sqrtx/#/description
补充一个python的实现:
class Solution:
def mySqrt(self, x: int) -> int:
r = x
while r * r > x:
r = (r + x // r) //
return int(r)
使用内置函数:
class Solution:
def mySqrt(self, x: int) -> int:
return int(x ** 0.5)
leetcode69的更多相关文章
- leetcode-69.x的平方根
leetcode-69.x的平方根 Points 二分查找 牛顿迭代 题意 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保 ...
- [Swift]LeetCode69. x 的平方根 | Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- leetcode69 X的平方根的几种解法
第一种自然就是调APi啦(手动滑稽) public int mySqrt(int x) { return (int)Math.sqrt(x); } 时间是52 ms,还超过了1/5的人呢 第二种 二分 ...
- 【leetcode-69】 x 的平方根
(主要是越界问题) 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 ...
- LeetCode69.x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- leetcode69. x 的平方根 🌟
题目: 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 ...
- LeetCode69 Sqrt(x)
题意: Implement int sqrt(int x). Compute and return the square root of x.(Medium) 分析: 二分搜索套路题,不能开方开尽的时 ...
- tusen 刷题
//1.single number和变体 //2.lru lfu 3.给一个正整数集合,求一个和最大且能被3整除的子集.Follow up: 如果集合里有正有负 4.leetcode200-numbe ...
- leetcode学习目录
1 leetcode-69. x 的平方根 https://www.cnblogs.com/shoshana-kong/p/9745424.html 2. 3. 4. 5. 6.
随机推荐
- python实现:将文本文件分割成多个小文本文件(php也可实现)
前两天有个朋友说,想实现一个文本文件按照固定行数进行分割成多个文本文件,却不知如何实现.如果数据量小手动分割下就好了,如果数据量很大的话手动完成实在太耗费人力了,也不现实.那么就需要借助脚本去实现.既 ...
- Bitwise Equations
Problem Description You are given two positive integers X and K. Return the K-th smallest positive i ...
- jquery属性值选择器
$("[attribute|='value']") 选择指定属性值等于给定字符串或改字符串为前缀(该字符串后跟一个连字符“-”)的元素. attribute: 一个属性名 valu ...
- bzoj1077
题解 这道题n的范围很小,所以我们可以考虑枚举+判定 设放在天平右边的是C,D. 以A+B<C+D为例,因为差分约束必须是差的形式,所以我们将式子变形 B−C<D−A然后枚举D,A的取值, ...
- DOM之概述
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 在JavaScript中进行文件处理,第一部分:基础
译注:原文是<JavaScript高级程序设计>的作者Nicholas Zakas写的,本翻译纯属为自己学习而做,仅供参考.原文链接:这里 很多年前,我在一次Goole面试被问到,如何在w ...
- Qt 编译完后指定输出路径
make install INSTALL_ROOT=/home/hotot/qt4rls
- IO包中的其他类总结
一.PrintStream和PrintWriter PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式. PrintStream 打印的所有字符都使用平台的默认字符 ...
- test20181021 快速排序
题意 对于100%的数据,\(n,m \leq 10^5\) 分析 考场上打挂了. 最大值就是后半部分和减前半部分和. 最小是就是奇偶相减. 方案数类似进出栈序,就是catalan数 线段树维护即可, ...
- 浅谈 Gevent 与 Tornado(转)
原文:http://www.pywave.com/2012/08/17/about-gevent-and-tornado/ 还是前几月的时候,几乎在同一时间,自己接触到了 Gevent 和 Torna ...