[leedcode 69] Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

public class Solution {
    //本题利用了牛顿迭代法:设r是f(x) = 0的根(x^2-k=f(x)),选取x0作为r初始近似值,过点(x0,f(x0))做曲线y = f(x)的切线L,L的方程为y = f(x0     //)+f‘(x0)(x-x0),求出L与x轴交点的横坐标 x1 = x0-f(x0)/f‘(x0),称x1为r的一次近似值。

    //过点(x1,f(x1))做曲线y = f(x)的切线,并求该切线与x轴交点的横坐标 x2 = x1-f(x1)/f‘(x1),称x2为r的二次近似值。重复以上过程,得r的近似值     //序列,其中x(n+1)=x(n)-f(x(n))/f‘(x(n)),称为r的n+1次近似值,上式称为牛顿迭代公式。
    //根据牛顿迭代的原理,可以得到以下的迭代公式:X(n+1)=[X(n)+p/Xn]/2
    //还可以用常识性知识考虑,x=(x+k/x)/2

    //本题注意点:
    //1.返回结果要先声明为double
    //2.需要检查参数的正负
    //3.注意1e-6的写法

    public int mySqrt(int x) {
        if(x<=0) return 0;
        double res=1.0;
        while(Math.abs(res*res-x)>1e-6){
            res=(res+x/res)/2;
        }
        return (int)res;
    }
}
时间: 2024-10-12 19:53:53

[leedcode 69] Sqrt(x)的相关文章

Leetcode 69. Sqrt(x) 求整数根 in Java

69. Sqrt(x) Total Accepted: 109623 Total Submissions: 418262 Difficulty: Medium Implement int sqrt(int x). Compute and return the square root of x. public class Solution { public int mySqrt(int x) { if(x==1) return 1; //注意此题返回值int,和sqrt返回值double不同 do

LeetCode --- 69. Sqrt(x)

题目链接:Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 这道题的要求是实现int sqrt(int x),即计算x的平方根. 考虑二分,即先令l和r分别为1和x/2+1(x的平方根一定小于等于x/2+1),然后m等于(l+r)/2,不断比较m*m和x的大小. 由于m*m的时候,可能溢出,因此可以用除法代替乘法,或者采用long long类型. 时间复杂度:O(logn) 空间复杂度:O(1)

69. Sqrt(x) &amp;&amp; 367. Valid Perfect Square

69. Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Binary Search Math Hide Similar Problems (M) Pow(x, n) (M) Valid Perfect Square Time Limit Exceed Solution if start from 0. public class Solution { public int m

69. Sqrt(x)

/* * 69. Sqrt(x) * 2016-5-10 by Mingyang * Just binary search and logn complexity */ public static int sqrt(int x) { long i = 0; long j = x / 2 + 1; while (i <= j) { long mid = (i + j) / 2; if (mid * mid == x) return (int)mid; if (mid * mid < x) i =

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

69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute and return the square root of x. 分析: 解法1:牛顿迭代法(牛顿切线法) Newton's Method(牛顿切线法)是由艾萨克·牛顿在<流数法>(M

LeeCode from 0 —— 69. Sqrt(x)

69. Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is retur

Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncat

69. Sqrt(x)(LeetCode)

Implement int sqrt(int x). Compute and return the square root of x. 1 class Solution { 2 public: 3 int mySqrt(int x) { 4 long r = x; 5 while (r*r > x) 6 r = (r + x/r) / 2; 7 return r; 8 } 9 };

leetcode 69 Sqrt(x) ---java

Implement int sqrt(int x). Compute and return the square root of x. 求一个数的平方根. 其实就是一个二分查找. 有两种查找方式 1.就是找到最大int值,然后从0到max,二分查找. 2.直接针对x,从0到x,二分查找. public class Solution { public int mySqrt(int x) { int max = 46340; int flag = max/2; if( x >= max*max )