leetcode power of two

https://leetcode.com/problems/power-of-two/

understanding:

我的方法就是取log,判断是不是整数。网上找到更好的办法一句话就完成。


best code:

url http://bookshadow.com/weblog/2015/07/06/leetcode-power-of-two/


class Solution:
    # @param {integer} n
    # @return {boolean}
    def isPowerOfTwo(self, n):
        return n > 0 and n & (n - 1) == 0



my code:

import math
class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False

        tmp = math.log(n)/math.log(2)
        print tmp
        a = int(tmp)
        b = tmp*10
        c = b - a*10
        print [a,b,c]
        if c > 1e-10:
            return False
        else:
            return True


你可能感兴趣的:(leetcode power of two)