实现 int sqrt(int x) 函数。

计算并返回 x 的平方根,其中 是非负整数。

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

示例 1:

输入: 4
输出: 2

示例 2:

输入: 8
输出: 2
说明: 8 的平方根是 2.82842...,
  由于返回类型是整数,小数部分将被舍去。
class Solution {
public int mySqrt(int x) {
if (x <= 0)return 0;
int low = 1;int hight = x;
while (low <= hight) {
long mid = (hight-low)/2+low;
if (mid*mid == x)return (int)mid;
else if (mid *mid < x)low = (int)mid+1;
else hight = (int)mid -1;
}
if (hight * hight < x)return (int)hight;
else return (int)low;
}
}

LeetCode69.x的平方根的更多相关文章

  1. leetcode-69.x的平方根

    leetcode-69.x的平方根 Points 二分查找 牛顿迭代 题意 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保 ...

  2. [Swift]LeetCode69. x 的平方根 | Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  3. leetcode69. x 的平方根 🌟

    题目: 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 ...

  4. leetcode69 X的平方根的几种解法

    第一种自然就是调APi啦(手动滑稽) public int mySqrt(int x) { return (int)Math.sqrt(x); } 时间是52 ms,还超过了1/5的人呢 第二种 二分 ...

  5. leetcode学习目录

    1  leetcode-69. x 的平方根   https://www.cnblogs.com/shoshana-kong/p/9745424.html 2. 3. 4. 5. 6.

  6. 【leetcode-69】 x 的平方根

    (主要是越界问题) 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 ...

  7. 牛顿法求平方根 scala

    你任说1个整数x,我任猜它的平方根为y,如果不对或精度不够准确,那我令y = (y+x/y)/2.如此循环反复下去,y就会无限逼近x的平方根.scala代码牛顿智商太高了println( sqr(10 ...

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

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

  9. hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***

    不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...

随机推荐

  1. express工程的优化和请求参数的处理

    1.让工程自动刷新 在Express的默认工程中,ejs, jade等模板的改变会立刻被渲染到浏览器中,但是js的改变不能立即刷新.这时候我们要用到一些自动刷新工具, 如 nodemon, super ...

  2. Exactly-Once 投递语义

    小结: 1.Exactly-Once 是指发送到消息系统的消息只能被消费端处理且仅处理一次,即使生产端重试消息发送导致某消息重复投递,该消息也在消费端也只被消费一次. 消息队列 RocketMQ &g ...

  3. 介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码

    http://gijgo.com   gijgo组件 特点:简易.文档全-虽然是英文的但是清晰易懂可读性强.含示例代码(后端直接用原生.Net C# MVC的哦!非常合.Net开发胃口),网站网速快, ...

  4. Chap1:基本概念[《区块链中文词典》维京&甲子]

  5. javaweb连接数据库并完成增删改查

    一.连接数据库 1.mysql数据库的安装和配置 在网上找到了篇关于mysql的安装详细说明,供读者自己学习 https://www.jb51.net/article/23876.htm 2.mysq ...

  6. scala-模式匹配

    option模式匹配: var map1=Map("abc"->5,"eee"->6) var x=map1.get("abc" ...

  7. python摸爬滚打之day11----函数闭包,迭代器

    1.函数名 函数名就是一个变量名, 函数名存储的是该函数的内存地址.    函数名都可以进行哪些应用? 函数名可以赋值给其他的变量; 函数名可以作容器里的元素使用; 函数名可以当做形参传进另一函数; ...

  8. canvas将图片转成base64格式 以及 验证图片是否透明

    logoImgUpload:function(file) { let self = this; self.formatUpload(file); let reader = new FileReader ...

  9. 【Python全栈-HTML】HTML如何做出分割线效果

    参考: https://blog.csdn.net/weixin_39198406/article/details/78827671 一.普通效果 <hr> <hr align=ce ...

  10. oracle中not in 和 in 的替代写法

    -- not in 的替代写法select col from table1 where col not in(select col from table2); select col,table2.co ...