无聊的刷题笔记

Power Of Two

LeetCode 231 LintCode 1314

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

Example 1:

Input: 1
Output: true

Example 2:

Input: 16
Output: true

Example 3:

Input: 218
Output: false
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n==0:
return False
while n&1==0:
n = n>>1
if n==1:
return True
else:
return False