实现 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) 求平方根的更多相关文章

  1. 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 ...

  2. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  3. [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 ...

  4. [转载]求平方根sqrt()函数的底层算法效率问题

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

  5. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  6. 141. Sqrt(x)【牛顿迭代法求平方根 by java】

    Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...

  7. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...

  8. note 5 二分法求平方根,素数,回文数

    +二分法求平方根 x = float(raw_input('Enter the number')) low = 0 high = x guess = (low + high ) / 2 if x &l ...

  9. 求平方根算法 Heron’s algorithm

    求平方根问题 概述:本文介绍一个古老但是高效的求平方根的算法及其python实现,分析它为什么可以快速求解,并说明它为何就是牛顿迭代法的特例. 问题:求一个正实数的平方根. 给定正实数 \(m\),如 ...

随机推荐

  1. 洛谷P2530 [SHOI2001]化工厂装箱员

    题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不 ...

  2. 【Lintcode】136.Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  3. BZOJ3938:Robot

    浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227.html 题目传送门:https://www.lydsy.com/JudgeOnline/proble ...

  4. rt-thread的定时器管理源码分析

    1 前言 rt-thread可以采用软件定时器或硬件定时器来实现定时器管理的,所谓软件定时器是指由操作系统提供的一类系统接口,它构建在硬件定时器基础之上,使系统能够提供不受数目限制的定时器服务.而硬件 ...

  5. valgrind 代码检查,内存泄漏

    使用平台 linux 下载 http://valgrind.org/ 文档 http://valgrind.org/docs/manual/manual.html 博客 https://www.osc ...

  6. 【opencv学习笔记二】opencv3.4.0组件结构说明

    在学习opencv使用之前我们先来看一下opencv有哪些组件结构.至于OpenCV组件结构的研究方法, 我们不妨管中窥豹,通过opencv安装路径下include目录里面头文件的分类存放,来一窥Op ...

  7. [poj1144]Network(求割点模板)

    解题关键:割点模板题. #include<cstdio> #include<cstring> #include<vector> #include<stack& ...

  8. 20.Consent Controller Get请求逻辑实现

    在这里之前讲的这一块的信息就要登场了 需要通过构造函数把这几个注入进来 县引入这三个命名空间 把这三个注入进来,这就是显示依赖 先通过returnUrl拿到这个Request request拿到后,就 ...

  9. 938. Range Sum of BST

    Given the root node of a binary search tree, return the sum of values of all nodes with value betwee ...

  10. Codeforces Round #459 (Div. 2)The Monster[匹配问题]

    题意 给一个序列,包含(,),?,?可以被当做(或者),问你这个序列有多少合法的子序列. 分析 n^2枚举每一个子序列,暂时将每个?都当做右括号,在枚举右端点的时候同时记录两个信息:当前左括号多余多少 ...