[LeetCode][JavaScript]Sqrt(x)

Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

https://leetcode.com/problems/sqrtx/



对于数学早就还给老师的我,开方真是跪了。

查了一下是牛顿迭代法(什么鬼。

先随便猜一个数,我就猜了三分之一,然后套用公式。

candidate = (candidate + x / candidate) / 2;

题目要求返回int型,我精度就算到了0.01。

查牛顿迭代法的途中看到一篇文章很有意思。

http://www.guokr.com/post/90718/

 1 /**
 2  * @param {number} x
 3  * @return {number}
 4  */
 5 var mySqrt = function(x) {
 6     var candidate = x / 3;
 7     while(Math.abs(x - candidate * candidate) > 0.01){
 8         candidate = (candidate + x / candidate) / 2;
 9     }
10     return parseInt(candidate);
11 };
时间: 2024-10-21 23:35:17

[LeetCode][JavaScript]Sqrt(x)的相关文章

[LeetCode][JavaScript]Pascal's Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

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)

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

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

[LeetCode][JavaScript]Count Primes

Count Prime Description: Count the number of prime numbers less than a non-negative number, n. https://leetcode.com/problems/count-primes/ 找出所有小于n的数中的质数. 删数法.开一个1到n的数组,删除所有2的倍数,3的倍数...直到√n的倍数,最后剩下的就是质数. 1 /** 2 * @param {number} n 3 * @return {number

[LeetCode][JavaScript]Bulb Switcher

Bulb Switcher There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start t

[LeetCode][JavaScript]Power of Two

Power of Two Given an integer, write a function to determine if it is a power of two. https://leetcode.com/problems/power-of-two/ 二分. 2的整数次幂,要么开方后是整数(这个数也是2的整数次幂),要么除以2之后再开方后是整数.继续递归判断开方后的结果直到碰到1或者2. 比如1024(2^10),是由两个32(2^5)相乘:2048(2^11),是由两个32(2^5)相

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