leetcode power of two

来源:互联网 发布:淘宝win10激活码哪里买 编辑:程序博客网 时间:2024/06/12 21:02

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 mathclass 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


0 0
原创粉丝点击