2015年7月5日星期日

LeetCode: Power of Two

Node: in Line 6, the type of m should be double or run time error when input is more than 65537(2^16 + 1). Because when m=65536, m*m is out of range of Integer.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n==0) return false;
        if(n==1) return true;
        double m=2;
        while(m*m<n){
            m=m*m;
        }
        if(n%(int)m != 0) return false;
        return isPowerOfTwo(n/m);
    }
};


没有评论:

发表评论