其他分享
首页 > 其他分享> > <Math> 50 367

<Math> 50 367

作者:互联网

50. Pow(x, n)

abs (Integer.MIN_VALUE) > Integer.MAX_VALUE

class Solution {
    public double myPow(double x, int n) {
        if(n == 0)
            return 1;
        if(n == Integer.MIN_VALUE){
            n = n/2;
            x = x*x;
        }
        if(n < 0){
            n = -n;
            x = 1/x;
        }
        return (n%2 == 0) ? myPow(x * x, n/2) : x*myPow(x * x, n/2);
    }
}

 

367. Valid Perfect Square

1. 从1搜索到 sqrt(num),看有没有平方正好等于 num 的数:

class Solution {
    public boolean isPerfectSquare(int num) {
        for(int i = 1; i <= num / i; i++){
            if( i * i == num) return true;
        }
        return false;
    }
}

 

标签:myPow,MIN,int,50,VALUE,num,Integer,367
来源: https://www.cnblogs.com/Afei-1123/p/11892336.html