Implement int sqrt(int x).
Compute and return the square root of x.
x is guaranteed to be a non-negative integer.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.
(1)暴力搜索

 class Solution {
public:
int mySqrt(int x) {
if(x<=)
return x;
int begin=;
int end=x;
int mid=;
while(begin<=end)
{
mid=(begin+end)/;
if(mid==x/mid)
return mid;
else if(mid<x/mid)
begin=mid+;
else
end=mid-;
}
return end;
}
};

(2)牛顿迭代

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

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

  1. [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 ...

  2. 069 Sqrt(x) 求平方根

    实现 int sqrt(int x) 函数.计算并返回 x 的平方根.x 保证是一个非负整数.案例 1:输入: 4输出: 2案例 2:输入: 8输出: 2说明: 8 的平方根是 2.82842..., ...

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

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

  4. LeetCode 69. Sqrt(x) (平方根)

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

  5. Java for LeetCode 069 Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. 解题思路一: public int mySqrt(int x) ...

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

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

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

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

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

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

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

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

随机推荐

  1. BZOJ4009:[HNOI2015]接水果

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

  2. bzoj 2039 & 洛谷 P1791 人员雇佣 —— 二元关系最小割

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2039 https://www.luogu.org/problemnew/show/P1791 ...

  3. 1. Two Sum[LeetCode 简单 by 大志]

    1. 二数之和 题目 English Given an array of integers, return indices of the two numbers such that they add ...

  4. HDOJ5438(图的各个连通分量遍历)

    #include<cstdio> #include<cstring> using namespace std; ; template<class T> struct ...

  5. 杂项:art-template-loader

    ylbtech-杂项:art-template-loader 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://www.npmjs.com/p ...

  6. error: converting to execution character set: Invalid or incomplete multibyte or wide character

    交叉编译.c文件,遇到如下问题 arm-linux-gcc -o show_lines show_lines.c -lfreetype -lm show_lines.c:199:19: error: ...

  7. Ubuntu14.04如何用root账号登陆系统

    在虚拟机VMWARE中安装完Ubuntu后,只能用新建的普通用户登陆,很不方便做实验:那如何用root用户登陆账号呢? (1)用普通账号登陆,打开终端terminal: (2)在terminal的输入 ...

  8. 【jQuery】slice()方法的使用

    [jQuery]slice()方法的使用  slice()方法:从已有的数组中返回选定的元素.  语法:          arrayObj.slice(start, end)             ...

  9. 问题:OAuth2.0;结果:帮你深入理解OAuth2.0协议

    1. 引言 如果你开车去酒店赴宴,你经常会苦于找不到停车位而耽误很多时间.是否有好办法可以避免这个问题呢?有的,听说有一些豪车的车主就不担心这个问题. 豪车一般配备两种钥匙:主钥匙和泊车钥匙.当你到酒 ...

  10. linux日常管理-rsync后台服务方式-2

    把本地的数据拷贝到远程 这里是个错误,read only.只允许读,不允许写. 改一下远程机器的配置文件 把read only改为no 拷贝到远程成功 tree一下远程机器的目录 ////////// ...