Implement int sqrt(int x).

Compute and return the square root of x.

简单的二分法,注意mid应该选为long,否则容易溢出:

 class Solution {
public:
int mySqrt(int x) {
if(x == || x == ) return x;
int beg = ;
int end = x;
long mid = ; //这里用long,否则会溢出
while(beg <= end){
mid = beg + (end - beg)/;
if(mid * mid < x){
beg = mid + ;
}else if(mid * mid > x){
end = mid - ;
}else{
return mid;
}
}
return end;
}
};

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. 0504-Hystrix保护应用-Hystrix Dashboard的使用与常见问题总结

    一.概述 Hystrix的主要优势之一是它收集的每个HystrixCommand的度量集合. Hystrix仪表板以高效的方式显示每个断路器的运行状况. 以前查看通过http://localhost: ...

  2. FPGA电源设计

    LDO(低压差线性稳压器),FPGA需要3.3V.2.5V和1.2V,可选用凌力尔特LINEAR:LT1083/84/85,低压差正压可调稳压器. 应用电路如图所示: 输入端加10UF电解电容,输出端 ...

  3. php foreach函数的用法

    php foreach函数用法举例.  Foreach 函数(PHP4/PHP5) foreach 语法结构提供了遍历数组的简单方式. foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类 ...

  4. sizeof 是编译时运算符

    typedef char RT1;typedef struct{ char a[2]; } RT2;template<typename T> RT1 test(typename T::X ...

  5. 快速搭建vue脚手架

    https://segmentfault.com/a/1190000011275993

  6. jupyter常用快捷键

    Jupyter Notebook 有两种键盘输入模式.即命令模式和编辑模式,这与 vim有些类似. 在编辑模式下,可以往单元中键入代码或文本,此时单元格被绿色的框线包围,且命令模式下的快捷键不生效. ...

  7. cdoj1339郭大侠与线上游戏

    地址:http://acm.uestc.edu.cn/#/problem/show/1339 题目: 郭大侠与线上游戏 Time Limit: 6000/2000MS (Java/Others)    ...

  8. $python虚拟化运行环境——virtualenv

    介绍 virtualenv是一种虚拟化环境,可以理解为创建了一个虚拟化的pyhon运行空间,可以从新安装各种库,而与本机环境互不影响,互相隔离. 安装及使用 首先要安装包管理工具pip(pip的使用详 ...

  9. 【Java】Swing+IO流实现一个简单的文件加密程序(较完整版)

    留着参考 beans package com.my.bean; import java.io.Serializable; public class EncryptedFile implements S ...

  10. Apache 服务常用命令

    # 查看编译的模块文件httpd -lapachectl -l # 查看apache版本信息,操作系统位数,apr版本 httpd -Vapachectl -V # 查看编译过的模块,并查看哪一个是 ...