069 Sqrt(x) 求平方根
实现 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) 求平方根的更多相关文章
- 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 ...
- [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, where x is guaranteed to be a no ...
- [转载]求平方根sqrt()函数的底层算法效率问题
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- 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; ...
- note 5 二分法求平方根,素数,回文数
+二分法求平方根 x = float(raw_input('Enter the number')) low = 0 high = x guess = (low + high ) / 2 if x &l ...
- 求平方根算法 Heron’s algorithm
求平方根问题 概述:本文介绍一个古老但是高效的求平方根的算法及其python实现,分析它为什么可以快速求解,并说明它为何就是牛顿迭代法的特例. 问题:求一个正实数的平方根. 给定正实数 \(m\),如 ...
随机推荐
- druid相关的时间序列数据库——也用到了倒排相关的优化技术
Cattell [6] maintains a great summary about existing Scalable SQL and NoSQL data stores. Hu [18] con ...
- EmbarassedBirds全体开发人员落泪
Github (李昆乘,赖展飞) 现阶段还在开发后期,API调试过程中. 本周无法上线. 全体开发人员留下眼泪. 贴上几个功能图, 给大家尝尝鲜吧! 现阶段仍在API调试 因为队员李昆乘经常出去玩没有 ...
- listen and translation exercise 52
Two years ago my husband bought me a bicycle. Twenty of the thirty-five years I ran my own business. ...
- 《java编程思想》读后笔记:一,标签
标签 是后面跟有冒号的标识符,格式如下: label : java中通过break与continue关键词可以完成类似于跳转的操作,其实现机制便是标签. 虽然很少有人使用,但是其有自身的适用场景:多层 ...
- JQuery添加删除标签
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <script src= ...
- 暑假集训Chapter1 贪心
为什么要今天写呢? 明天全力研究Johnson和一些奇奇怪怪的贪心 今天把能写的都写了 T1T2太水了直接上代码吧 #include<bits/stdc++.h> #define LL l ...
- 每天一个Linux命令:目录
版权声明 更新:2017-04-19博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 在使用Linux的过程中总是发现有一些L ...
- [HAOI 2011] Problem A
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2298 [算法] 考虑用总人数 - 最多人说真话 显然 , 对于每个人 , 如果他说的 ...
- 如何得到WPF中控件绑定的EventTrigger
System.Windows.Interactivity.Interaction.GetTriggers(sender as DependencyObject)[0].Actions
- TCP 协议的消息
TCP: SYN ACK FIN RST PSH URG 详解 TCP的三次握手是怎么进行的了:发送端发送一个SYN=1,ACK=0标志的数据包给接收端,请求进行连接,这是第一次握手:接收端收到请求并 ...