https://oj.leetcode.com/problems/sqrtx/

求一个数的开方,二分查找一个数,直到这个数的平方 - target 小于 0.001。

但这道题出的不好,返回值竟然是 int ,脑洞太大了。

class Solution {
public:
int sqrt(int x) {
if(x<=)
return ;
if(x == )
return ;
double fi = 0.0001;
//binary search
double low = , high = x, middle = x/;
while()
{
if(abs(middle*middle - x) < fi)
return middle;
if(middle*middle > x)
high = middle;
else if(middle*middle<x)
low = middle;
middle= (low + high)/2.0;
}
}
};

LeetCode OJ-- Sqrt(x) *的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

随机推荐

  1. Golang tcp转发 remoteAddr错误

    Golang tcp 转发 第一版本 accept获取的Conn里的localAddr做为源地址,remoteAddr来做为目的地址 // tcpForward package main import ...

  2. leetcode-8-pointer

    void deleteNode(ListNode* node) { *node = *node->next; }

  3. python模块之datetime

    相比于time模块,datetime模块的接口则更直观.更容易调用 datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day: ...

  4. JAVA基础篇—异常

    五种常见异常 1.NullPointerException 空指针 2.ClassNotFoundException 指定类不存在 3.ArithmeticException运算异常 4.ArrayI ...

  5. centos7 安装显卡驱动方法

    方法一: 首先需要添加一个第三方的源ELRepo.这个源支持RED HAT系的Linux系统,主要是提供一些硬件的驱动程序.这个源的主页如下: http://elrepo.org/tiki/tiki- ...

  6. x mod a=r(N对a,r)

    //模数不一定互质,互质才可以用孙子定理. /* https://blog.csdn.net/zmh964685331/article/details/50527894 uu遇到了一个小问题,可是他不 ...

  7. debian7安装icedove

    stable --icedove --esr $ cat /etc/apt/sources.list | grep "deb http://security.debian.org/ whee ...

  8. 解决子线程操作UI的方法

  9. 【LVS】简介与说明

    一.IPVS的三种负载均衡技术 通过NAT实现虚拟服务器(VS/NAT) 客户通过Virtual IP Address(虚拟服务的IP地址)访问网络服务时,请求报文到达调度器,调度器根据连接调度算法从 ...

  10. powerdesigner约束名唯一出错的解决办法

    powerdesigner中自动生成的约束名有时会因为表的前缀一样而不具有唯一性,这样在生成时就会出错,一般的解决办法有以下两种: 1.模型=>Reference中可以看到当前模型中的所有Ref ...