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 ...
随机推荐
- 全自动Web后门扫描(转)
阅读目录 工具介绍 使用方法 工具介绍 这是一款全自动Web后门查杀工具,基于Python开发 某些较新的后门可能会查杀失败 规则列表来自seay博客 回到顶部 使用方法 1.按恶意代码查杀: pyt ...
- 浅析Java中的final关键字(转)
谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法.下 ...
- 启示—地点IT高管20在职场心脏经(读书笔记6)
启示--一个IT高管20在职场心脏经 第七章 关于销售 用"最"来形容公司的销售.能够用上若干的词汇: 最牛,最累,最精,最傻,最有钱,最贱,最能吹.最能装... 1.1 销售 ...
- Pro Aspnet MVC 4读书笔记(4) - Working with Razor
Listing 5-1. Creating a Simple Domain Model Class using System; using System.Collections.Generic; us ...
- shell 监控局域网的主机是否up(转)
#!/bin/bash for ((i=30;i<60;i++)) ;do ping -c 3 172.31.0.$i>/dev/null #ping -c 172.31.0.30 ~17 ...
- Hadoop之——HBase注意事项
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46447573 1.HBase(NoSQL)的数据模型 1.1 表(table) 存 ...
- react.js 从零开始(六)Reconciliation
Reconciliation React 的关键设计目标是使 API 看起来就像每一次有数据更新的时候,整个应用重新渲染了一样.这就极大地简化了应用的编写,但是同时使 React 易于驾驭,也是一 ...
- 杭州电acm理工大舞台版
我要参加全国软件设计大赛C/C++学生语言组,前一个假设<C训练和演习,并总结手>没看完,请阅读上述并根据所作的训练,然后做下面的练习. 门户:http://blog.csdn.net/l ...
- oracle11g的dmp文件导入oracle10g当误差:头验证失败---解决
原创作品,离 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ ...
- bigdata_ambari修改hiveserver_metastore链接库(从0.14 升级到1.2.1 )
第一步:[db升级 ,先看第二步] cd到 hive的 metastore upgrade目录 cd /usr/hdp/2.5.0.0-1245/hive/scripts/metastore/upg ...