当前位置:   article > 正文

LeetCode69. x 的平方根(C++)

LeetCode69. x 的平方根(C++)

LeetCode69. x 的平方根

题目链接

https://leetcode.cn/problems/sqrtx/description/
在这里插入图片描述

代码

class Solution {
public:
    int mySqrt(int x) {
        int right = x, left = 0, ans = -1;
        while(left <= right){
            long long mid = left + (right - left) / 2;
            if(mid * mid <= x){
                ans = mid;
                left = mid +1;
            }
            else{
                right = mid - 1;
                
            }
        }
        return ans;

    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/151168
推荐阅读
相关标签
  

闽ICP备14008679号