[LeetCode] Sqrt(x) 二分搜索
并未使用牛顿迭代,实现是通过二分搜索实现的,需要注意判断时候 x* x<K 容易溢出,所以可以改为 x< k/x。
#include <iostream>
using namespace std; class Solution {
public:
int sqrt(int x) {
if(x <) return x;
int lft = ,rgt = x;
int mid = (lft+rgt)/;
while(lft+<rgt){
if(mid == x/mid) return mid;
if(mid < x/ mid){
lft = mid;
}
else{
rgt = mid;
}
mid = (lft+rgt)/;
}
if( rgt<=mid/rgt) return rgt;
return lft;
}
}; int main()
{
Solution sol;
for(int i =;i<;i++)
cout<<sol.sqrt(i)<<endl;
return ;
}
[LeetCode] Sqrt(x) 二分搜索的更多相关文章
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- Leetcode Sqrt(x)
参考Babylonian method (x0 越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...
- leetcode—sqrt
1.题目描述 Implement int sqrt(int x). Compute and return the square root of x. 2.解法分析 很明显,用二分搜索可解,但是 ...
- [leetcode]Sqrt(x) @ Python
原题地址:https://oj.leetcode.com/problems/sqrtx/ 题意: Implement int sqrt(int x). Compute and return the s ...
- [Leetcode] sqrt 开根号
Implementint sqrt(int x). Compute and return the square root of x. 题意:求根号下x 的值 思路:使用二分搜索.先定义x平方根的取值区 ...
- LeetCode: Sqrt
Title: Implement int sqrt(int x). Compute and return the square root of x. 思路:这个平方根肯定是在[1,x]之间,所以在这个 ...
- LeetCode:Sqrt(x) 解题报告
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. SOLUTION 1: 参见:二分法总结,以及模 ...
- LeetCode——Sqrt(x)
Description: Implement int sqrt(int x). Compute and return the square root of x. 好好学习数学还是非常有用的,牛顿迭代法 ...
- [LeetCode] Insert Interval 二分搜索
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- 裸机——RTC
1. 首先晓得RTC的基本知识 RTC被划分到timer,但RTC是面向时间点的. 如果按照定时器的思路去思考,那么应该考虑 时间周期 和 计数值. RTC 不是面向时间点的,所以略有不同, 时间周期 ...
- python基础之生成器、三元表达式、列表生成式、生成器表达式
生成器 生成器函数:函数体内包含有yield关键字,该函数执行的结果是生成器,生成器在本质上就是迭代器. def foo(): print('first------>') yield 1 pri ...
- Apache服务配置
Apache 1.安装Apache服务 第1步:把光盘设备中的系统镜像挂载到/media/cdrom目录. [root@zhangjh ~]# mkdir -p /media/cdrom/ [root ...
- gcc常用命令
1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...
- CSS3 Shape ---使用形状改变旁边内容的布局
注意 本文所实现的功能,在浏览器的支持上并不好,除chrome浏览器外其余的大部分浏览器均不支持,虽然可以使用polyfill解决,但也不能很好的支持,有时也会出错 polyfill使用方法 下载po ...
- 6.bootstrap 将文本内容关联一个动作(手机端导航适配)&在超小尺寸下显示,屏幕变大后消失
1.情景:这是出现在手机端导航适配的,点击文本MENU可以出现下拉的list 解决方法: 1.首先要想到,MENU只有两个状态,因此可以用checkbox实现 2.将MENU放在label标签里面,l ...
- mutable c++
The keyword mutable is used to allow a particular data member of const object to be modified. This i ...
- 启动zookeeper报错:JAVA_HOME is not set
启动zookeeper时报错JAVA_HOME is not set 看了环境变量,确实配置好了,但是zookeeper竟然没找到 修改bin目录下的zkEnv.cmd关于jdk的一部分 set JA ...
- apache的/etc/httpd/conf/httpd.conf和/usr/local/apache2/conf/httpd.conf区别
一.问题 centos系统用yum安装完apache后,重启后有时会失效,然后去网上找资料,发现有的说重启命令是这样的: /etc/init.d/httpd restart 而有的呢,说重启命令应该是 ...
- loadrunner破解出现“license security violation,Operation is not allowed”的错误提示
1.关闭loadrunner,将破解文件(“lm70.dll”.“mlr5lprg.dll”)放置在LoadRunner\bin下面 2.以管理员身份运行loadrunner,在CONFUGURATI ...