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. hadoop集群环境搭建准备工作

    一定要注意hadoop和linux系统的位数一定要相同,就是说如果hadoop是32位的,linux系统也一定要安装32位的. 准备工作: 1 首先在VMware中建立6台虚拟机(配置默认即可).这是 ...

  2. PrintWriter 和 BufferedWriter 写入文件.

    Ref: should I use PrintWriter to wrap BufferedWriter? The main reason for using PrintWriter is the w ...

  3. SQL SERVER 查看死锁的存储过程

    end

  4. 在eclipse下面搭建Clojure开发运行环境

    打开eclipse,点击菜单栏“help->Install New Software...", 然后,点击”add“, 在Location处输入 http://ccw.cgrand.n ...

  5. 数据库导出导入操作(expdp,impdp)

    EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. 命令行: sqlplus/nolog connect username/password as sysd ...

  6. 【BZOJ2809】【splay启发式合并】dispatching

    Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...

  7. JSP执行过程

    JSP执行流程: @1.客户端发出请求. @2.Web容器将JSP转译成Servlet源代码. @3.Web容器将产生的源代码进行编译. @4.Web容器加载编译后的代码并执行. @5.把执行结果响应 ...

  8. Python自动化运维之27、Django(一)

    一.概述 1.什么是框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单说就是使用别人搭好的舞台,你来做表演. 2.常 ...

  9. 如何在search中动态的显示和隐藏tree中的字段

    在tree定义 invisible 来自context <field name="country_id" invisible="context.get('invis ...

  10. python的min()函数也可用于比较tuple

      python的min()函数也可用于比较tuple >>> a = (2,'asv','dfg') >>> b = (3,'gsg','weg') >&g ...