列表

详情


FED26. 平方根

描述

请补全JavaScript函数,要求返回数字参数的平方根。

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1738ms, 内存消耗: 77856KB, 提交时间: 2021-12-16

{"css":"","js":"function _sqrt(number) {\n    // 补全代码\n\n if(number === 1 || number === 0) return number;    \n var low = 0;   \n var high = number;   \n while(low < high){        \n \tvar p = (low + high)>>1;        \n    if( p * p === number)            \n    \treturn p;      \n    else if(p * p < number)           \n    \tlow = p;        \n    else          \n    \thigh = p;  \n}\n}\n\n  \n\n","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77772KB, 提交时间: 2022-01-24

{"css":"","js":"function _sqrt(number) {\n    // 补全代码\n     var x=Math.sqrt(number);\n    return x;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1743ms, 内存消耗: 78064KB, 提交时间: 2021-12-15

{"css":"","js":"function _sqrt(number) {\n    // 补全代码\n    if(number === 1 || number === 0) return number;\n    let lower = 0;\n    let high = number;\n    while(lower < high) {\n        let p = (lower + high) >> 1;\n        if(p * p === number) {\n            return p;\n        } else if (p * p < number) {\n            lower = p;\n        } else {\n            high = p;\n        }\n    }\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77852KB, 提交时间: 2021-12-15

{"css":"","js":"function _sqrt(number) {\n    // 补全代码\n    if(number === 1 || number == 0) {\n        return number;\n    }\n    let l = 0;\n    let h = number;\n    while(l < h){       \n    let p = (l + h)>>1;       \n    if( p * p === number)           \n        return p;     \n    else if(p * p < number)          \n        l = p;       \n    else         \n        h = p;   \n    }\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77860KB, 提交时间: 2021-12-26

{"css":"","js":"function _sqrt(number) {\n    // 补全代码\n    var x=Math.sqrt(number);\n    return x;\n}","html":"","libs":[]}

上一题