Title:

Implement int sqrt(int x).

Compute and return the square root of x.

思路:这个平方根肯定是在【1,x】之间,所以在这个区间使用二分查找。需要注意的是,代码中一直使用mid ,x/mid来比较,因为如果使用mid的平法,即使long long都会越界

class Solution {
public:
int mySqrt(int x) {
if(x<=)
{
return x;
} int left = ;
int right = x; while(left<=right)
{
int mid = (left + right)/;
if(mid == x/mid)
{
return mid;
}
else if(mid < x/mid)
{
left = mid + ;
}
else
{
right = mid - ;
}
} return right;
}
};

还可以使用牛顿迭代法

http://blog.csdn.net/doc_sgl/article/details/12404971

class Solution {
public:
int mySqrt(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (x ==0)
return 0;
double pre;
double cur = 1;
do
{
pre = cur;
cur = x / (2 * pre) + pre / 2.0;
} while (abs(cur - pre) > 0.00001);
return int(cur);
}
};

  

LeetCode: Sqrt的更多相关文章

  1. [LeetCode] Sqrt(x) 求平方根

    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 ...

  3. Leetcode Sqrt(x)

    参考Babylonian method  (x0  越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...

  4. leetcode—sqrt

    1.题目描述   Implement int sqrt(int x).   Compute and return the square root of x. 2.解法分析 很明显,用二分搜索可解,但是 ...

  5. LeetCode:Sqrt(x) 解题报告

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. SOLUTION 1: 参见:二分法总结,以及模 ...

  6. LeetCode——Sqrt(x)

    Description: Implement int sqrt(int x). Compute and return the square root of x. 好好学习数学还是非常有用的,牛顿迭代法 ...

  7. [Leetcode] sqrt 开根号

    Implementint sqrt(int x). Compute and return the square root of x. 题意:求根号下x 的值 思路:使用二分搜索.先定义x平方根的取值区 ...

  8. [LeetCode] Sqrt(x) 二分搜索

    Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Math Binary Search     ...

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. 以“图片渐入渐出”为例讲述jQuery插件的具体实现

    首先声明,此代码以网友“斯迈欧”原创作为此例的讲解: 在这之前我们先看看我们要做的效果是什么样的: 解析下面的样式:我们要图片在过“一定时间”后自动切换,在右下角处有小方块似数字1,2,3,4,这些数 ...

  2. unity与Android相互调用

    原地址:http://www.cnblogs.com/ayanmw/p/3727782.html 现在unity 导出的android客户端需要调用 Android 的支付SDK,但是unity与an ...

  3. linux源码阅读笔记 fork函数

    在阅读源码的过程中,发现找不到fork函数的定义.后来在linux/init/main.c中找到了这样一条语句 static inline _syscall0(int,fork) 原来这里就是fork ...

  4. POJ2411 Mondriaan's Dream 轮廓线dp

    第一道轮廓线dp,因为不会轮廓线dp我们在南京区域赛的时候没有拿到银,可见知识点的欠缺是我薄弱的环节. 题目就是要你用1*2的多米诺骨排填充一个大小n*m(n,m<=11)的棋盘,问填满它有多少 ...

  5. javascript中li标签的排序和数组sort的用法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. Git 使用方法

    Git 常用命令 git init here -- 创建本地仓库(repository),将会在文件夹下创建一个 .git 文件夹,.git 文件夹里存储了所有的版本信息.标记等内容 git remo ...

  7. DataTime.Now.Ticks的应用

    参考:http://www.cnblogs.com/delphinet/archive/2011/06/09/2075985.html 转换成秒: using System; using System ...

  8. EXCEL排序

    题目描述:     Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能.     对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始).随后在 N ...

  9. jdbc的通讯录CRUD

    基于JDBC的通讯录练手:项目以MVC模式开发,包名:cn.itcast.txl.domain;cn.itcast.txl.dao;cn.itcast.txl.dao.impl;cn.itcast.t ...

  10. intellij idea 注释行如何自动缩进?

    我们指代代码注释的快捷键是 Ctrl+/ 但是加注释后"//"自动放在行首,没缩进,就像这样