Skip to main content

Leetcode - 69. Sqrt(x)

· One min read

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

Solution 1.

/**
* @param {number} x
* @return {number}
*/
var mySqrt = function (x) {
let result = 0;

if (x === 0) {
return 0;
}

if (x <= 2) {
return 1;
}

for (let i = 0; i < x; i++) {
if (i * i > x) {
result = i - 1;
break;
}
}

return result;
};