加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Leetcode 69 Sqrt(x)

发布时间:2020-12-13 21:18:32 所属栏目:PHP教程 来源:网络整理
导读:Implement int sqrt(int x) . Compute and return the square root of x . 求x的平方根。 2分没甚么好说的,注意INT_MAX溢出的情况! class Solution {public: int mySqrt(int x) { long long l=0,r=x,mid; while(l=r) { mid=(l+r)1; long long ans=mid*mid;

Implement int sqrt(int x).

Compute and return the square root of x.

求x的平方根。

2分没甚么好说的,注意INT_MAX溢出的情况!

class Solution { public: int mySqrt(int x) { long long l=0,r=x,mid; while(l<=r) { mid=(l+r)>>1; long long ans=mid*mid; if(ans<=x && (mid+1)*(mid+1)>x) return mid; if(ans>x) r=mid⑴; else l=mid+1; } return mid; } };


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读