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\),如 ...
随机推荐
- Java_HTTP_01_HttpClient
一. 二.参考文档 1. HttpClient官方文档 HttpClient官方文档中文翻译 1.HttpClient 4 实现文件下载 2.httpclient 上传文件.下载文件 3.httpcl ...
- Python: scikit-image Blob detection
这个用例主要介绍利用三种算法对含有blob的图像进行检测,blob 或者叫斑点,就是在一幅图像上,暗背景上的亮区域,或者亮背景上的暗区域,都可以称为blob.主要利用blob与背景之间的对比度来进行检 ...
- bjwc Day1 暴力大战
今天终于有题了... 题目是COCI2016/2017 Round #4 T1一看就是NP问题,k<=50,开始想暴力,想了个n^4的,大概能过,就没去管它 T2想得太naive,丢了100分给 ...
- HL7 V2 分隔符
Delimiter Characters Character Purpose 0x0D Marks the end of each segment. | Composite delimiter. ^ ...
- 11 Vue学习 headtop
1: HeaderTop.vue : 面包屑:el-breadcrumb 定义面包屑, separator是分隔符. el-breadcrumb-item: 是面包屑中用 分隔符 分开的多 ...
- Servlet拦截方式
Servlet拦截方式 1.拦截固定后缀的url,比如设置为 *.do.*.action, 例如:/user/add.action 此方法最简单,不会导致静态资源(jpg,js,css)被拦截. 2. ...
- LeafLet之气泡框隐藏"x"图标
例子:var marker.bindPopup( "我是一个图标的文本", { minWidth: 300 }).openPopup();L.Popup Constructor(函 ...
- Hibernate注解详细介绍
引自http://blog.csdn.net/lin_yongrui/article/details/6855394 声明实体Bean @Entity public class Flig ...
- Spring3注解零配置
我们在以前学习 Spring 的时候,其所有的配置信息都写在 applicationContext.xml 里,大致示例如下: java代码: 查看复制到剪贴板打印 OracleDriver& ...
- adt eclipse 配置问题 error:could not open ...jvm.cfg
在安装adt eclipse后,打开eclipse提示error:could not open eclipse\jre\lib\amd64\jvm.cfg 据网上显示原因应该是以前安装过java,然后 ...