leetcode[71] Sqrt(x)
题目,就是实现一个开方,返回是整数。int sqrt(int x)
用二分法,因为一个数的开方肯定小于 x/2 + 1, 因为小于5的某些数的开方并不一定比x/2小,所以要+1,那么们定义一个left一个right分别为0和x/2 + 1,然后更新左右边界,直至左边界大于右边界,返回右边界就是答案。
class Solution {
public:
int sqrt(int x) {
long long left = ;
long long right = x/ + ;
while(left <= right)
{
long long tmp = (left + right)/ * ((left + right)/); // 一定要注意括号,后面的括号要是少了就错了,因为会先乘再除2结果就不一样
if (tmp == x) return (left + right)/;
else if (tmp > x)
right = (left+right)/ - ;
else
left = (left+right)/ + ;
}
return right;
}
};
还有种用牛顿迭代法。
leetcode[71] Sqrt(x)的更多相关文章
- LeetCode 71.简化路径
LeetCode 71.简化路径 题目描述: 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径.在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此 ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- 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) (平方根)
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)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
- 【leetcode】Sqrt(x)
题目描述: Implement int sqrt(int x). Compute and return the square root of x. 实现开根号,并且返回整数值(这个很重要,不是整数的话 ...
- Java for LeetCode 069 Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. 解题思路一: public int mySqrt(int x) ...
- (二分查找 拓展) 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 ...
随机推荐
- 中国的手写输入法iOS8.1在崩溃
当中国的手写输入法.会导致app收起.于debug时刻.报错: 2014-10-22 14:45:10.269 App[524:170755] -[UIKBBlurredKeyView candida ...
- Scut游戏server引擎Unity3d访问
Scut提供Unity3d Sdk包.便利的高速发展和Scut游戏server对接: 看Unity3d示为以下的比率: 启动Unity3d项目 打开Scutc.svn\SDK\Unity3d\Asse ...
- Down to the TLP: How PCI express devices talk (Part II)
http://xillybus.com/tutorials/pci-express-tlp-pcie-primer-tutorial-guide-2 Data Link Layer Packets A ...
- 60分钟Python快速学习(转)
60分钟Python快速学习(给发哥一个交代) 阅读目录 第一步:开发环境搭建: 第一个Python功能:初识Python 02.Python中定义变量不需要数据类型 03.在Pythod中定义方法 ...
- MLAPP——概率机器学习知识汇总
<机器学习>课程使用Kevin P. Murphy图书<Machine Learning A Probabilistic Perspective>本英语教材,本书从一个独特的数 ...
- Log4jdbc demo
package log4jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import org.junit.Te ...
- 设计模式Template Method模式(Template Method)摘录
23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例.怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将实例化托付给还 ...
- JavaScript之包装对象
JavaScript对象是一种复合值:它是属性和已命名值的集合.通过"."符号来引用属性值.当属性值是一个函数时,称为方法. ①一段你常用但却未必明白其真正底层原理的代码: var ...
- Robot Framework自动化测试(一)---第一个脚本(转)
最近工具中用Robot Framework框架来做自动化,所以,花时间学习了一下. =======所需环境=================== Python: https://www.python. ...
- 每天进步一点点-->功能fseek() 使用方法
在阅读代码时,遇到了非常早之前用过的fseek(),非常久没实用了.有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头.须要包括头文件stdio.h fseek 函数名: fsee ...