69. Sqrt(x)

Easy

Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

Input: 4
Output: 2

Example 2:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since
  the decimal part is truncated, 2 is returned.
package leetcode.easy;

public class SqrtX {
@org.junit.Test
public void test() {
long x1 = 4;
long x2 = 8;
System.out.println(mySqrt(x1));
System.out.println(mySqrt(x2));
} public int mySqrt(long x) {
for (long i = 0; i <= x; i++) {
if (i * i == x) {
return (int) i;
} else if (i * i < x && (i + 1) * (i + 1) > x) {
return (int) i;
} else {
continue;
}
}
return 0;
}
}

LeetCode_69. Sqrt(x)的更多相关文章

  1. 速算1/Sqrt(x)背后的数学原理

    概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...

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

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

  3. Leetcode 69. Sqrt(x)

    Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...

  4. 欧几里得证明$\sqrt{2}$是无理数

    选自<费马大定理:一个困惑了世间智者358年的谜>,有少许改动. 原译者:薛密 \(\sqrt{2}\)是无理数,即不能写成一个分数.欧几里得以反证法证明此结论.第一步是假定相反的事实是真 ...

  5. 求sqrt()底层效率问题(二分/牛顿迭代)

    偶然看见一段求根的神代码,于是就有了这篇博客: 对于求根问题,通常我们可以调用sqrt库函数,不过知其然需知其所以然,我们看一下求根的方法: 比较简单方法就是二分咯: 代码: #include< ...

  6. 【leetcode】Sqrt(x)

    题目描述: Implement int sqrt(int x). Compute and return the square root of x. 实现开根号,并且返回整数值(这个很重要,不是整数的话 ...

  7. Leetcode Sqrt(x)

    参考Babylonian method  (x0  越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...

  8. Sqrt(x) - LintCode

    examination questions Implement int sqrt(int x). Compute and return the square root of x. Example sq ...

  9. 3.Sqrt(x)

    要求:Implement int sqrt(int x).  Compute and return the square root of x. 解决方法: 1.牛顿法(Newton's method) ...

随机推荐

  1. PHP启动php-fpm成功,但php-cgi进程查找不到 502 getaway

    一般情况大家刚把lnmp环境安装好之后,把nginx中 fastcgi_pass unix:/tmp/php-cgi.sock项修改成 fastcgi_pass 127.0.0.1:9000之后,网页 ...

  2. PL/SQL 使用控制流程

    一.条件分支语句 1.if判断 IF <布尔表达式> THEN PL/SQL 和 SQL语句 END IF; 2.if else判断 IF <布尔表达式> THEN PL/SQ ...

  3. springboot整合shiro引用配置文件配置redis信息报空指针异常

    1.问题现象: 上面这些属性是从application.properties配置文件中获取的,按常理来说应该能顺利获取到,但均未赋上值. 2.解决办法:(不得不说百度,千篇一律,最后用谷歌找到的) 最 ...

  4. 网站性能优化——DNS预热与合并HTTP请求

    DNS预热 一次DNS解析耗时20-120ms, 当网页中使用的域名较多时,DNS预热节省的时间还是非常可观的 先看效果 预热的目的: 减少请求次数 提前对DNS预获取 预热的方式 爬虫 APP 网页 ...

  5. Django后台中文乱码

    无论如何,刚开始一定要写上默认编码utf8!!!!!! 第一种办法: 检查 ...\Lib\site-packages\Django-1.10.2-py2.7.egg\django\conf\loca ...

  6. [HDU 5608]Function(莫比乌斯反演 + 杜教筛)

    题目描述 有N2−3N+2=∑d∣Nf(d)N^2-3N+2=\sum_{d|N} f(d)N2−3N+2=∑d∣N​f(d) 求∑i=1Nf(i)\sum_{i=1}^{N} f(i)∑i=1N​f ...

  7. 三十五.MySQL读写分离 MySQL多实例 、MySQL性能调优

    1.实现MySQL读写分离 搭建一主一从结构 配置maxscale代理服务器 测试分离配置   1.1 搭建一主一从结构 192.168.4.51 主 192.168.4.52 从 测试OK   1. ...

  8. Linux操作系统常用命令合集——第六篇-软件包操作(2个命令)

    一.前言介绍 软件包即程序包 程序包管理 关键词:rpm程序包管理.YUM仓库管理.源码编译安装 程序包管理: 将编译好的应用程序的各组成文件打包一个或几个程序包文件,从而方便快捷地实现程序包的安装. ...

  9. 【原创】go语言学习(十三)struct介绍2

    目录: 方法的定义 函数和方法的区别 值类型和指针类型 面向对象和继承 结构体和json序列化 方法的定义 1.和其他语言不一样,Go的方法采⽤用另外一种方式实现. package main impo ...

  10. express中的中间件(middleware)、自定义中间件、静态文件中间件、路由中间件

    express文档地址 什么是中间件呢(middleware)?它是谁的中间件呢? 首先我们需要了解到请求和响应, 请求就是客户端发送请求给服务器, 响应就是,服务器根据客户端的请求返回给客户端的数据 ...