首页 > 其他 > 详细

Leetcode 231 Power of Two

时间:2015-07-06 17:38:11      阅读:217      评论:0      收藏:0      [点我收藏+]

Given an integer, write a function to determine if it is a power of two.

所有为power of two的整数均为 100000... 的形式,减一则会造成所有位相异,即与的结果为0。

这里需要排除当n为0的情况,减一后与的结果仍为0。

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

 

Leetcode 231 Power of Two

原文:http://www.cnblogs.com/lilixu/p/4624726.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!