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. 运行hadoop自带的wordcount例子程序

    1.准备文件 [root@master ~]# cat input.txt hello java hello python hello c hello java hello js hello html ...

  2. day11 函数和命名空间

    # def my_sum(*args):# sum_2=0# for i in args:# sum_2+=i# return sum_2### ret=my_sum(1,2,3,4)# print( ...

  3. 20包含min函数的栈

      题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.   建一个辅助栈,把每次最小最小的元素(之前最小元素,与当前新入栈的元素比较)放在辅助栈里.   import j ...

  4. 二叉树遍历,递归,栈,Morris

    一篇质量非常高的关于二叉树遍历的帖子,转帖自http://noalgo.info/832.html 二叉树遍历(递归.非递归.Morris遍历) 2015年01月06日 |  分类:数据结构 |  标 ...

  5. Calendar的那些神坑

    参考我的博客:http://www.isedwardtang.com/2017/08/31/java-calendar-bug/

  6. 使用Vue.js初次真正项目开发-2018/07/14

    一.组件化 使用Vue.js进行开发,按照MVVM模式,围绕数据为核心,进行开发. 开发过程根据业务和功能组件化,组件化一方面让我们开发思路更加清晰,另一方面对于数据的处理和控制变得更加简单,毕竟一个 ...

  7. 回溯算法 DFS深度优先搜索 (递归与非递归实现)

    回溯法是一种选优搜索法(试探法),被称为通用的解题方法,这种方法适用于解一些组合数相当大的问题.通过剪枝(约束+限界)可以大幅减少解决问题的计算量(搜索量). 基本思想 将n元问题P的状态空间E表示成 ...

  8. 华为交换机S5700系列配置通过STelnet登录设备示例

    配置通过STelnet登录设备示例 组网图形 图1 配置用户通过STelnet登录设备组网图 在服务器端生成本地密钥对 <HUAWEI> system-view [HUAWEI] sysn ...

  9. 1001: [BeiJing2006]狼抓兔子

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 12827  Solved: 3044[Submit][ ...

  10. 深入理解JVM 垃圾收集器(上)

    HotSpot虚拟机中的垃圾收集器 GC评价标准 GC调优 响应时间 吞吐量 1.新生代收集器 Serial收集器 ParNew收集器 Parallel Scavenge收集器 2.老年代收集器 Se ...