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; ...
随机推荐
- 简单易懂dubbo入门实例
一.创建Maven多模块项目 项目结构如下 模块介绍: dubbo-api ----API接口 dubbo-consumer ----消费者 dubbo-provider ...
- HDOJ(1069)最长下降子序列
每个箱子可有3种叠加方式,所以有3*n个箱子.将箱子按长度由大到小排序,有求箱子按宽度的最长下降子序列的高度之和即可. #include<cstdio> #include<algor ...
- C# 播放音乐
用 .NET 自带的类库 System.Media 下面的 SoundPlayer 来播放音乐的方式,此种方式使用托管代码,应该是更为可取的方式吧 使用起来非常简单,下面稍作说明: . 支持同步.异步 ...
- CentOS7 搭建LNMP
一. 安装依赖文件 1. sudo yum install gcc gcc-c++ zlib zlib-devel libxml2 libxml2-devel openssl open ...
- Boost Python学习笔记(四)
你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...
- 【WIP】客户端JavaScript 事件处理
创建: 2017/10/15 完成: 2017/10/15 更新: 2017/11/04 加粗事件的参数 更新: 2017/12/12 增加事件处理时获取事件对象的方法 更新: 2019/05/2 ...
- MATLAB求解非线性方程组
matlab中有专门的solve函数来解决方程组的(a-x)^2+(b-y)^2=e^2(C-x)^2+(D-y)^2=v^2已知a,b,c,d,e,v 值求解 X,Y 请问用 matlab 如何写, ...
- 2018牛客多校第九场E(动态规划,思维,取模)
#include<bits/stdc++.h>using namespace std;const long long mod=1000000007,inv=570000004;long l ...
- oracle函数获取汉字拼音的首字母
CREATE OR REPLACE FUNCTION F_TRANS_PINYIN_CAPITAL(P_NAME IN VARCHAR2) RETURN VARCHAR2 AS V_COMPARE V ...
- linux下oracle一些常用命令
dbca 配置数据库netca 配置tnslsnrctl status tns状态lsnrctl stop TNS停止lsnrctl start TNS启动