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; ...
随机推荐
- vs code 安装Scala
首先本机要安装scala(官网肿么下不了,CSDN上面下的): 配置scala到环境变量PATH中(Scala的根目录): VS中安装以下扩展: 1. Scala: 2. Sbt: 3. Code R ...
- BZOJ1206:[HNOI2005]虚拟内存
我对模拟的理解:https://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...
- vi查找替换命令详解
一.查找 查找命令 /pattern<Enter> :向下查找pattern匹配字符串 ?pattern<Enter>:向上查找pattern匹配字符串 使用了查找命令之后,使 ...
- Poj 2299 Ultra-QuickSort(归并排序求逆序数)
一.题意 给定数组,求交换几次相邻元素能是数组有序. 二.题解 刚开始以为是水题,心想这不就是简单的冒泡排序么.但是毫无疑问地超时了,因为题目中n<500000,而冒泡排序总的平均时间复杂度为, ...
- HDOJ1025(最长上升子序列)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDOJ1166(线段树点修改)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Java类的生命周期(转)
引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内java方面的教材大多只是告 ...
- python 基础 字典 小例子
统计单词次数 作为字典存储 cotent = "who have an apple apple is free free is money you know" result = { ...
- BAT小米奇虎美团迅雷携程等等各大企业校招,笔试面试题。
类似在线测试的方式展示题目. 历年在线笔试试卷: 百度 http://www.nowcoder.com/paper/search?query=%E7%99%BE%E5%BA%A6 腾讯http:// ...
- mysql--二进制日志(bin-log)
一.设置二进制日志 进制日志记录了所有的DDL和DML,但不包括各种查询.通过二进制日志,可以实现什么效果呢?二进制日志文件可以[实现灾难数据恢复],另外可以应用到[mysql复制数据同步].二进制日 ...