LeetCode----Power of Two
来源:互联网 发布:mac笔记本怎么清理垃圾 编辑:程序博客网 时间:2024/06/05 10:53
Power of Two
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
分析:
简单题,判断一个数是否是2的倍数。我先统计出给定数的二进制中1的个数,如果个数大于1,则为False。
相关学习链接:快速求二进制中1的个数,位运算。
代码:
class Solution(object): def isPowerOfTwo(self, n): """ :type n: int :rtype: bool """ if n <= 0: return False count = 0 while n: n &= n - 1 count += 1 if count == 1: return True else: return False
0 0
- [LeetCode] Power of Two
- 【LeetCode】Power of Two
- [Leetcode]Power of Two
- [leetCode] Power of Two
- leetcode --Power of Two
- leetcode: Power of Two
- LeetCode Power of Two
- [LeetCode] Power of Two
- LeetCode Power of Two
- [leetcode] Power of Two
- [Leetcode]Power of Two
- Leetcode: Power of Two
- [LeetCode]Power of Two
- leetcode | Power of Two
- 【leetcode】Power of Two
- [LeetCode]Power of Two
- [leetcode] Power of two
- LeetCode Power of Two
- 在Linux下判断系统当前是否开启了超线程
- 黑马程序员——IO流之IO包中的其他类
- CSerialPort类的使用
- WifiStateTracker
- 算法复杂度的计算
- LeetCode----Power of Two
- windows下安装redis(Redis服务端按照 非常简单)
- 一些l练习about Java
- span加display:block才能显示
- ThreadPoolExecutor使用简介
- 15年IT经验,如何从一无所有成为上市公司高层!
- nyoj--171--聪明的kk(动态规划--博弈)
- SQLite 操作方法
- WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path