Implement int sqrt(int x).

Compute and return the square root of x.

题目大意:实现求一个int的根。

解题思路:二分。

public class Solution {
public int mySqrt(int x) {
if (x <= 0) {
return 0;
}
if (x <= 3) {
return 1;
}
long num = x / 2;
int tmp = x;
while (num * num >= tmp) {
if (x / 2 == 0) {
break;
}
num = x / 2;
x /= 2;
}
long low = num, high = num * 2, mid = 0;
while (low <= high) {
mid = (low + high) >> 1;
if (mid * mid <= tmp && (mid + 1) * (mid + 1) > tmp) {
break;
} else if (mid * mid > tmp) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return (int) mid;
}
}

Sqrt(x)——LeetCode的更多相关文章

  1. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

  2. LeetCode 解题报告索引

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

  3. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  4. arts-week12

    Algorithm 69. Sqrt(x) - LeetCode Review Cloudflare goes InterPlanetary - Introducing Cloudflare's IP ...

  5. arts-week11

    Algorithm 69. Sqrt(x) - LeetCode Review Building a network attached storage device with a Raspberry ...

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

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  7. 【一天一道LeetCode】#69. Sqrt(x)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

  8. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  9. LeetCode算法题-Sqrt(Java实现)

    这是悦乐书的第158次更新,第160篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第17题(顺位题号是69). 计算并返回x的平方根,其中x保证为非负整数. 由于返回类型 ...

随机推荐

  1. HttpContext.Current

    HttpContext. Response 直接这样写会报错 是因为 httpcontext没有提供response 这个静态的方法. 通过这样写就可以 ASP.NET还为它提供了一个静态属性Http ...

  2. 自定义HtmlHelper方法

    原文:http://www.cnblogs.com/wenjiang/archive/2013/03/30/2990854.html HtmlHelper方法是ASP.NET MVC中非常强大的特性, ...

  3. 偶遇问题 - - JavaScript 取消链接默认行为问题

    今天在测试<JavaScript DOM编程艺术(第2版)>中第69页代码时,遇到了问题.本来预期效果应该是点击链接后不跳转当前页面,而是另外弹出有个窗口.但结果却是页面跳转了.代码如下图 ...

  4. Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 英文帮助文档 分类: ASP.NET 2014-10-28 10:50 147人阅读 评论(1) 收藏

    Security Switch 4.2 =================== Security Switch enables various ASP.NET applications to auto ...

  5. 控件AutoCompleteTextView实现动态匹配输入内容的一种输入框

    <AutoCompleteTextView android:layout_width="match_parent" android:layout_height="w ...

  6. HBuilder使用感受

    最近公司在考虑搞HTML5和后台交互的架构,我于是便下载了HBuilder使用,这里分享下我偶的使用感受. 一.首先,下载下来是一个压缩包,解压后是可以直接使用的,这让我对它的第一感觉很好.不用安装, ...

  7. NetAdvantage webdatagrid 控件的一些属性

    属性: 1 behaviors 行为下的属性集合 Row Selectors 主要用于设置行选择样式与形为的集合 Enable 属性表示是否启用 Row Selectors下的属性设置 RowNumB ...

  8. VisualStudio2013内置SQLServer入门(二)--增删改查

    前一篇 http://www.cnblogs.com/qixi233/p/4766451.html 这篇是sqlserver的操作 界面比较走心哈哈哈,将就着看,主要就是下面增删改查四个btn 对于s ...

  9. POJ2104 K-th Number 静态区间第k最值 平方分割

    干掉这道题的那一刻,我只想说:我终于**的AC了!!! 最终内存1344K,耗时10282ms,比起归并树.划分树以及其他各种黑科技,这个成绩并不算光彩⊙﹏⊙ 但至少,从最初的无数次TLE到最终的AC ...

  10. 371. Sum of Two Integers -- Avota

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...