implement Math.sqrt()
2022年5月1日小于 1 分钟
implement Math.sqrt()
Question
Math.sqrt() helps us getting the square root of a number.
Can your write your own mySqrt()
? You should return the integer part only, truncating the fraction part.
mySqrt(0)
// 1
mySqrt(1)
// 1
mySqrt(2)
// 1
mySqrt(4)
// 2
mySqrt(NaN)
// NaN
Attention for the special case listed up in the spec.
Follow-up
What is time & space complexity of your solution ? Can you do better?
Code
/**
* @param {any} x
* @return {number}
*/
function mySqrt(x) {
// your code here
}