lintcode-141-x的平方根
141-x的平方根
实现 int sqrt(int x) 函数,计算并返回 x 的平方根。
样例
sqrt(3) = 1
sqrt(4) = 2
sqrt(5) = 2
sqrt(10) = 3挑战
O(log(x))
标签
数学 二分法 脸书
思路
参考http://blog.csdn.net/lyy_hit/article/details/49785677
使用二分法,将 mid*mid 的值与 x 比较,但为了防止计算评方时发生溢出,二分上限为 46341 而非 x 的值。
又因为待求数的平方根有可能是介于两个整数之间的,这种情况需要做一下判断处理,否则会陷入死循环中。
code
class Solution {
public:
/**
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) {
// write your code here
int low = 0, high = 46341, mid = 0;
while(low <= high) {
if (mid == low + (high-low)/2) {
break;
}
mid = low + (high-low)/2;
int square = mid * mid;
if(square == x) {
return mid;
}
else if(x > square){
low = mid;
}
else {
high = mid;
}
}
return mid;
}
};
lintcode-141-x的平方根的更多相关文章
- 141 x的平方根
原题网址:http://www.lintcode.com/zh-cn/problem/sqrtx/ 实现 int sqrt(int x) 函数,计算并返回 x 的平方根. 您在真实的面试中是否遇到过这 ...
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- 141. Sqrt(x)【牛顿迭代法求平方根 by java】
Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- 牛顿法求平方根 scala
你任说1个整数x,我任猜它的平方根为y,如果不对或精度不够准确,那我令y = (y+x/y)/2.如此循环反复下去,y就会无限逼近x的平方根.scala代码牛顿智商太高了println( sqr(10 ...
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
随机推荐
- 模拟MBR Grub故障修复
1. MBR故障修复 备份 mkdir /pp mount /dev/sdb1 /pp dd if=/dev/sda of=/pp/mrb.bak bs=512 count=1 破坏mrb dd ...
- Elasticsearch 聚合操作
数据准备: PUT /shop { "settings": { "number_of_shards": 3, "number_of_replicas& ...
- checked和unchecked转换
static unsafe void Main(string[] args) { unchecked //checked 运行时候引发异常 { int n = int.MaxValue; n++; C ...
- QK对中断的特殊处理
1.QK的特性 QK(Quntum Kernel)是一个抢占式.基于优先级实时微内核.一个多任务调度器: QK不同于传统的RTOS,是非阻塞的,并且只用了一个stack: 对QK中的任务来说,采用了I ...
- 树莓派如何连接WIFI
hello,大家好,我是叶秋! 上一期呢,给大家介绍了如何给树莓派安装系统,有细心的朋友就会发现上一期安装系统的文章漏了一点点知识,不知道机智的你是否有发现呢~~(尴尬
- Java多线程一些基础知识
最近复习了一些多线程方面的基础知识,做一下总结,多以自己的理解来文字叙述,如果有漏点或者理解错的地方,欢迎各位大佬多多指出: ps:线程分为用户线程和守护线程,当程序中的所有的用户线程都执行完了之后, ...
- Redis的n种妙用,不仅仅是缓存
redis是键值对的数据库,常用的五种数据类型为字符串类型(string),散列类型(hash),列表类型(list),集合类型(set),有序集合类型(zset) Redis用作缓存,主要两个用途: ...
- 北京Uber优步司机奖励政策(3月12日~3月13日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 优步UBER司机高峰小时保底奖励
高峰小时保底奖励 (此奖励仅针对6月1日及以后激活的优步车主): 得到“高峰小时保底”的前提为: 1. 在规定高峰时间段内(不得跨段计算),任意一小时内至少完成一单: 2. 在规定高峰时间段内,任 ...
- Qt的4个图像类QImage/QPixmap/QBitmap/QPicture 转
Qt的4个图像类QImage/QPixmap/QBitmap/QPicture 转 (一)QPixmap和QImage的区别 http://www.thisisqt.com/forum/viewthr ...