实现 int sqrt(int x) 函数。
计算并返回 x 的平方根。
x 保证是一个非负整数。
案例 1:
输入: 4
输出: 2
案例 2:
输入: 8
输出: 2
说明: 8 的平方根是 2.82842..., 由于我们想返回一个整数,小数部分将被舍去。
详见:https://leetcode.com/problems/sqrtx/description/

Java实现:

方法一:暴力解

class Solution {
public int mySqrt(int x) {
if(x<0){
return x;
}
int begin=1;
int end=x;
int mid=0;
while(begin<=end){
mid=(begin+end)>>1;
if(mid==x/mid){
return mid;
}else if(mid<x/mid){
begin=mid+1;
}else{
end=mid-1;
}
}
return end;//结束条件end一定<begin,所以返回end
}
}

方法二:牛顿迭代法

class Solution {
public int mySqrt(int x) {
if(x<0){
return x;
}
long v=x;
while(v*v>x){
v=(v+x/v)/2;
}
return (int)v;
}
}

069 Sqrt(x) 求平方根的更多相关文章

  1. LeetCode 069 Sqrt(x) 求平方根

    Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negati ...

  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). Compute and return the square root of x, where x is guaranteed to be a no ...

  4. [转载]求平方根sqrt()函数的底层算法效率问题

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

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

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

  6. 141. Sqrt(x)【牛顿迭代法求平方根 by java】

    Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...

  7. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...

  8. note 5 二分法求平方根,素数,回文数

    +二分法求平方根 x = float(raw_input('Enter the number')) low = 0 high = x guess = (low + high ) / 2 if x &l ...

  9. 求平方根算法 Heron’s algorithm

    求平方根问题 概述:本文介绍一个古老但是高效的求平方根的算法及其python实现,分析它为什么可以快速求解,并说明它为何就是牛顿迭代法的特例. 问题:求一个正实数的平方根. 给定正实数 \(m\),如 ...

随机推荐

  1. nginx websocket

    前两天折腾了下socketio,部署完发现通过nginx代理之后前端的socket无法和后端通信了,于是暴查一通,最后解决问题: location / { proxy_pass http://127. ...

  2. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...

  3. python raw string

    path = r'C:\a\b\c.txt' r'字符串' 是raw 字符串的意思, 其中的字符串不会转义,即不解释 \ . 作用之一:可以用来保存Windows的路径,直接从资源管理器复制来粘贴,不 ...

  4. BZOJ_2957_楼房重建_线段树

    BZOJ_2957_楼房重建_线段树 Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多 ...

  5. django 多数据库配置

    在django项目中, 一个工程中存在多个APP应用很常见. 有时候希望不同的APP连接不同的数据库,这个时候需要建立多个数据库连接. 1. 修改项目的 settings 配置 在 settings. ...

  6. BZOJ1382:[Baltic2001]Mars Maps

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  7. Grunt:常见错误

    ylbtech-Grunt:常见错误 1.返回顶部 1. D:\lab6\DS.JZB.Web>grunt build Running "clean:dist" (clean ...

  8. Django简单快速实现PUT、DELETE方法

    使用django的小伙伴们应该都知道我们是无法开心的处理PUT跟DELETE的 $.ajax({ url: 'XXX', type: 'PUT', dataType: 'json', data: { ...

  9. how to run faster

    题目大意: 已知 $$ b_i = \sum_{j=1}^n {(i,j)^d [i,j]^c x_j}$$,给定 $b_i$ 求解 $x_i$ 解法: 考虑 $f(n) = \sum_{d|n}{f ...

  10. Newtonsoft.Json null值不序列化

    如果对当前序列化的实体全部生效的话使用如下: var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandl ...