Leetcode 69 Sqrt(x) 二分查找(二分答案)
可怕的同时考数值溢出和二分的酱油题之一,常在各种小公司的笔试中充当大题来给你好看。。。
题意很简单,在《二分查找综述》中有描述。
重点:使用简单粗暴的long long来避免溢出,二分均方根的答案来得到准确解。
当然这里的溢出不止是相乘的溢出,还有第六行那段代码的溢出,每次都会有几个解决问题的斗士牺牲在这里。。。
class Solution {
public:
int mySqrt(int x) {
long long a = , b = x;
while(a <= b ){
long long mid = a + (b - a) /;
if(mid * mid == x) return mid;
if(mid * mid < x ) a = mid + ;
else b = mid - ;
}
return (int)b;
}
};
这里已经给出了两种题目的解法,相信大家对于二分查找有了一个比较清晰的认识,可以写一个关于二分查找的模板了,但是懒惰楼主是不会写的。。。
Leetcode 69 Sqrt(x) 二分查找(二分答案)的更多相关文章
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- (二分查找 拓展) leetcode 69. Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- [LeetCode] 69. Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- LeetCode 69. Sqrt(x) (平方根)
Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
- [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- 【leetcode边做边学】二分查找应用
很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...
- python函数(4):递归函数及二分查找算法
人理解循环,神理解递归! 一.递归的定义 def story(): s = """ 从前有个山,山里有座庙,庙里老和尚讲故事, 讲的什么呢? ""& ...
随机推荐
- MySQL的limit查询优化
MySQL的limit查询优化以下的文章主要是对MySQL limit查询优化的具体内容的介绍,我们大家都知道MySQL数据库的优化是相当重要的.其他最为常用也是最为需要优化的就是limit.MySQ ...
- 基于VC的声音文件操作(四)
(五)读取wav的实例 跟据WAVE文件的格式,实现了读取双声道立体声数据的例子如下: BYTE * GetData(Cstring *pString) //获取声音文件数据的函数,pString参数 ...
- Entity Framework EF6使用 MySql创建数据库异常解决办法
EF6使用MySQL数据库时,第一次创建数据库出现“Specified key was too long; max key length is 767 bytes”错误,解决办法请见以下连接. htt ...
- LintCode 111 Climbing Stairs
这道题参考了这个网址: http://blog.csdn.net/u012490475/article/details/48845683 /* 首先考虑边界情况,当有1层时,有一种方法. 然后再看2层 ...
- 2.一起来学hibernate之配置文件1与持久化对象
学框架,配置都是不可少的,有了配置,框架才知道我们想做什么,才知道如何去执行我们需要的操作! hibernate的配置文件,总体来说分为两个部分: 1.主配置文件hibernate.cfg.xml文件 ...
- typeid详解(转)
(http://www.cppblog.com/smagle/archive/2010/05/14/115286.html) 在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time ...
- .Net Core CLI windows安装
下载官方的msi安装包official MSI installer双击安装就可以了. 默认会安装到C:\Program Files\dotnet\目录下,如果找不到可以用命令 where dotnet ...
- Web Essentials之Bundling
返回Web Essentials功能目录 本篇目录 介绍 样例文件 已知行为 介绍 这篇要讲的是Bundling,我看很多人把它翻译为捆绑,如果你喜欢你也可以这么理解,我是不太习惯,我还是喜欢它为bu ...
- Java语法糖2:自动装箱和自动拆箱
前言 一开始想学学自动拆箱和自动装箱是被这个名字吸引到,听上去好像很高端的样子,其实自动拆箱.自动装箱是很简单的内容. 自动拆箱和自动装箱 Java为每种基本数据类型都提供了对应的包装器类型.举个例子 ...
- 作业二:Github注册过程
第一步.打开Github官网https://github.com/ ,在相应位置填写注册名.注册邮箱.注册密码完成后点击注册. 第二步.这时会弹出一个界面,让你选择你的私人计划(personal pl ...