Leetcode 367 Valid Perfect Square

来源:互联网 发布:网络诈骗案例大全 编辑:程序博客网 时间:2024/06/08 01:57

Leetcode 367 Valid Perfect Square

#include <math.h>class Solution {public:      int mySqrt(int x) {        //return (int)sqrt((double)x);        //使用牛顿法  x2 = x1 - f(x1)/f'(x1)   f(x) = x^2        double xn = 0;        double xn1 = x;        while (abs(xn1 - xn) > 0.000001){            xn = xn1;            xn1 =( xn / 2 + x / (2 * xn));        }        return (int)(xn1);    }      bool isPerfectSquare(int num) {    int root = mySqrt(num);    return (root *root == num);    }};