LeetCode 069 Sqrt(x) 求平方根
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) 求平方根的更多相关文章
- [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 ...
- 069 Sqrt(x) 求平方根
实现 int sqrt(int x) 函数.计算并返回 x 的平方根.x 保证是一个非负整数.案例 1:输入: 4输出: 2案例 2:输入: 8输出: 2说明: 8 的平方根是 2.82842..., ...
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- 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 ...
- Java for LeetCode 069 Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. 解题思路一: public int mySqrt(int x) ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- [转载]求平方根sqrt()函数的底层算法效率问题
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- 141. Sqrt(x)【牛顿迭代法求平方根 by java】
Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...
- 19. 求平方根序列前N项和
求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...
随机推荐
- javascript:delete 删除对象的属性
delete 运算符删除对以前定义的对象属性或方法的引用. 不可以删除的如下: 1通过var定义的变量 var a=1;delete a//false 2 声明后的函数 function a(){}; ...
- [bzoj2142]礼物(扩展lucas定理+中国剩余定理)
题意:n件礼物,送给m个人,每人的礼物数确定,求方案数. 解题关键:由于模数不是质数,所以由唯一分解定理, $\bmod = p_1^{{k_1}}p_2^{{k_2}}......p_s^{{k_ ...
- asp.net异常处理和错误页配置
最近做一个项目,直接拷贝了前辈写的程序,结果报错了查了半天都没查出原因,也看不出哪里报错,最后发现有一个错误被try...catch了,所以我们做项目的时候一般不需要try...catch. 假设所有 ...
- C#调试信息打印到输出窗口
System.Diagnostics.Debug.WriteLine("aaaa");
- MongoDB分析工具之二:MongoDB分析器Profile
MongoDB优化器profile 在MySQL 中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB 中是否有类似的功能呢?答案是肯定的,那就是MongoDB Database Prof ...
- openstackM版本安装
部署期间常见问题:http://www.cnblogs.com/bfmq/p/6001233.html,问题跟对架构的理解永远比部署重要!你玩技术是绝对是要基于理论的 一.基本情况:物理设备:4台惠普 ...
- C#事件触发机制
C#的事件触发机制,类似于c++的回调函数机制 我先简单说一下,委托和事件的实质,后期再重开一篇博文来详细说 委托:指向方法的指针,类似于C的函数指针 事件:是一个可以存放0个或多个方法指针的数据结构 ...
- python 基础 列表生成式
data = {'a':'abc';'b':'bac','c':'cba'} [v for k,v in data] 结果 ['abc','bca','cba'] 格式 [x for x in 内容 ...
- linux日常管理-top动态查看负载
动态查看负载命令,具体哪个程序,哪个进程造成的系统负载. top 回车查看 3秒更新一次 第一行和uptime和w第一行显示的一样. CPU使用率,us sy 内存相关,Mem 一共多少,使用了多少, ...
- qt数据库sql语句使用c++中的变量
void SerialWidget::on_btnMysql_clicked() { qDebug()<<QSqlDatabase::drivers()<<endl; /*列出 ...