1 Star 0 Fork 0

孙启萌 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
69.x-的平方根.js 836 Bytes
一键复制 编辑 原始数据 按行查看 历史
孙启萌 提交于 2022-04-11 14:03 . init初始化刷题仓库
/*
* @lc app=leetcode.cn id=69 lang=javascript
*
* [69] x 的平方根
*/
// @lc code=start
/**
* @param {number} x
* @return {number}
*/
var mySqrt = function(x) {
if (x === 0 || x === 1) {
return x
}
let minNum = 0
let maxNum = x
let half = Math.floor((minNum + maxNum) / 2)
while( !(half * half <= x) || !(x < (half + 1) * (half + 1)) ) {
if (half * half < x) {
minNum = half
half = Math.floor((minNum + maxNum) / 2)
} else if ( half * half > x ) {
maxNum = half
half = Math.floor((minNum + maxNum) / 2)
}
}
return half
};
// @lc code=end
/*
牛顿迭代法是比较好的,代码简洁效率高,但是重点在于数学基础,我不会
采用二分法搜索
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqm147896325/leetcode.git
git@gitee.com:sqm147896325/leetcode.git
sqm147896325
leetcode
leetcode
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891