LeetCode OJ-- Sqrt(x) *
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) *的更多相关文章
- LeetCode OJ 题解
		博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ... 
- 【LeetCode OJ】Interleaving String
		Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ... 
- 【LeetCode OJ】Reverse Words in a String
		Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ... 
- LeetCode OJ学习
		一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ... 
- 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 ... 
- 备份LeetCode OJ自己编写的代码
		常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ... 
- 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 ... 
- 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 ... 
- 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 ... 
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
		Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ... 
随机推荐
- 【思维题  并查集  图论】bzoj1576: [Usaco2009 Jan]安全路经Travel
			有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ... 
- jenkins+svn+pipeline+kubernetes部署java应用(二)
			在jenkins中只能通过http的方式获取svn的数据,所以需要配置svn的http访问方式 一.安装http服务端和mod_dav_svn插件 由于Subversion需要版本化的控制,因此标准的 ... 
- destoon手机端mobileurl函数增加城市分类参数
			mobileurl函数在include/global.func.php 858行 共四个参数,moduleid-模型id,catid-分类id,itemid -文章id,page-页码 functio ... 
- Linux虚拟机里用X11协议打开图形界面的eclipse
			1.下载工具包 XLaunch(安装到win)https://xming.en.softonic.com/ Eclipse IDE for C/C++ Developers(虚拟机里解压到 /data ... 
- 18/07/2017 R matrix
			矩阵:二维数组,元素拥有相同模式(数值型,字符型或者逻辑型) mymatrix <- matrix (vector, nrow_num_of_rows, ncol_num_of_columns, ... 
- #2 create and populate a database && realistic and practical applications
			The Chapter3 & Chapter4 of this book tells you how to create a realistic app on the web through ... 
- C语言的文件处理
			所谓“文件”一般指存储在外部介质上数据的集合.根据数据的组织形式,可分为ASCII文件和二进制文件.ASCII文件,又称为文本文件,它的每一个字节存放一个ASCII代码,代表一个字符.二进制文件是把内 ... 
- AtCoder Regular Contest 064 F - Rotated Palindromes
			Problem Statement Takahashi and Aoki are going to together construct a sequence of integers. First, ... 
- 利用Python访问Mysql数据库
			首先要明确一点,我们在Python中需要通过第三方库才能访问Mysql. 有这样几种方式:Mysql-python(即MySQLdb).pymysql.mysql-connector.Mysql-py ... 
- 【Luogu】P3809后缀排序(后缀数组模板)
			题目链接 今天终于学会了后缀数组模板qwq 不过只会模板emmmm 首先我们有一本蓝书emmmmmm 然后看到蓝书221页代码之后我就看不懂了 于是请出rqy rqy: 一开始那是个对单个字符排序的操 ... 
