LeetCode_sqrt(x)
class Solution {
public:
int sqrt(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x < ) return -;
if(x == ) return ;
if(x >= && x <= ) return ;
double start = 2.0;
double next = 0.5 *(start + x/start);
while(abs(start * start - x) > 0.000001){
start = next;
next = 0.5 *(start + x/start);
}
return int(start);
}
};
两种方法的详细分析: http://www.cnblogs.com/AnnieKim/archive/2013/04/18/3028607.html
LeetCode_sqrt(x)的更多相关文章
- 【leetcode】69-Sqrt(x)
problem Sqrt(x) code class Solution { public: int mySqrt(int x) {// x/b=b long long res = x;// while ...
随机推荐
- Filter Conditions 过滤条件
<pre name="code" class="html">Filter Conditions 过滤条件: Rsyslog 提供4种不同类型的&qu ...
- alias, bg, bind, break, builtin, caller, cd, command,
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command, compgen, complete, com ...
- encodeURI和encodeURIComponent的比较
encodeURI和encodeURIComponet函数都是javascript中用来对URI进行编码,将相关参数转换成UTF-8编码格式的数据.URI在进行定位跳转时,参数里面的中文.日文等非AS ...
- P - Atlantis - hdu1542(求面积)
题意:rt 求面积......不计算重复面积(废话..)hdu1255 的弱化版,应该先做这道题在做那道题的. ******************************************** ...
- svn版本信息自动更新到源代码
背景:在线上代码和线下代码不一致时,用这个时间先比对是很重要的,首先看时间确定是不是最新的代码,再进行问题排查是很有必要的.Subversion 的 svn:keywords 是一个非常有用的功能, ...
- 执行npm安装模块的命令 Cannot find module
npm 安装了 appium 和 appium-doctor 运行命令,appium-doctor 提示找不到模块: C:\Users\autotest>appiummodule.js:471 ...
- 轻量级mvvm Web开发框架 postby:http://zhutty.cnblogs.com
今天特别郁闷,怎么说呢,之前一直就用angular,然后这两天用的是avalon这东西,反正,一开始没时间去玩它,第一个任务就是封装个jq插件,实现一个小功能.反正呢,就是越写越郁闷.用过angula ...
- CVE-2015-8660分析
0x00测试环境 使用环境 备注 操作系统 Ubuntu15.04 虚拟机 内核版本 3.19.0-15-generic 漏洞来源 /fs/overlayfs/inode.c Before 201 ...
- MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql
Mysql 默认是没有开启这个权限的(只允许使用 host:localhost,或者 host:127.0.0.1),如果想用 host:192.168.1.* ,来访问mysql ,需要手动开启这个 ...
- 让 asp.net mvc 支持 带有+ _ 等特殊字符的路由
最近配置微信 业务域名 时,需要在服务器的根目录中上传一个文本文件,而这个文本文件需要放这样的目录中: 于在就在 服务器目录中创建了对应的文件夹,并将kuPv.txt上传,但是访问时,却怎么也访问不到 ...