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;
    }
}

 

上一篇:【LeetCode】50.Pow(x,n)反复平方法解决快速幂问题


下一篇:LeetCode题解(python)-50. Pow(x, n)