Implement int sqrt(int x).

Compute and return the square root of x.

牛顿迭代法, 碉堡了。

class Solution {
public:
    int sqrt(int x) {
        double ans = x;
        while (abs(ans * ans - x) > 0.0001) {
            ans = (ans + x / ans) / 2;
        }
        return (int)ans;
    }
};

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-10-07
  • 2021-07-31
猜你喜欢
  • 2022-01-25
  • 2021-05-16
  • 2021-08-18
  • 2021-10-05
  • 2022-02-10
  • 2022-12-23
相关资源
相似解决方案