实现 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. python与pip安装

    # Install pip for 2.7 and then python 2.7 itself sudo apt install python-pip sudo apt install python ...

  2. java设计模式--单例

    GOF23(group of four)---由4个大牛总结的JAVA23种常用的设计模式,归根结底都是为了代码的可扩展性. 设计模式中一种比较重要的思想就是:开闭原则,尽量做到对扩展开放,对修改关闭 ...

  3. [adminstrative][CentOS] CentOS 7 常用操作笔记

    CentOS从6换到7, 还没有系统的学习.虽然主要用的是systemd,但还是有一下特有的区别,会逐步整理如下: 官网文档索引:https://access.redhat.com/documenta ...

  4. dispatch_barrier_async,加锁

    dispatch_barrier_async用于文件读写,在写的时候,不允许进行读操作,在写操作完成才能进行读取操作,为了保证线程安全 加锁的方式: 1. barrier 2. NSLock 3. @ ...

  5. sql server创建临时表的两种写法和删除临时表

    --创建.删除临时表 --第一种方式 create table #tmp(name varchar(255),id int) --第二种方式 select count(id) as storyNum ...

  6. 2018/05/02 每日一学Linux 之 .bash_profile和.bashrc的区别

    最近一直在学习其他,导致博客就疏忽了,很不好(其实就是自己懒了......). -- 为什么要使用 .bash_profile和.bashrc ? 在平常的使用中,有些文件夹或者命令很长,在执行时需要 ...

  7. mysql报错Establishing SSL connection without server's identity verification is not recommended

    使用mysql数据库时报错:Establishing SSL connection without server's identity verification is not recommended ...

  8. idea+maven+springboot+mybatis+springmvc+shiro

    springboot就是把创建项目简单化,省去了以往的配置mybatis.springmvc的繁琐过程. 搭建web应用三个主要功能,请求和响应,数据库交互,权限配置. 一.idea创建项目 (1) ...

  9. shiro入门实例,基于ini配置

    基于ini或者关系数据库的,其实都是一样的,重要的是思想. # ==================================================================== ...

  10. Aop的基本介绍

    基本概念 通知  就是你想要的功能,也就是我们常说的安全.事物.日志等.先定义好这些,然后再想用的地方用一下.包含Aspect的一段处理代码 注意:其实这些功能(通知)并不是我们业务逻辑所必须的,只是 ...