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

 

posted @ 2019-11-19 20:57  阿飞哦  阅读(95)  评论(0编辑  收藏  举报