惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
IT之家
IT之家
Vercel News
Vercel News
C
Check Point Blog
Google DeepMind News
Google DeepMind News
月光博客
月光博客
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog

博客园 - db2zos

从假后端到 Cloudflare D1:一次医疗旅游网站的重构实践 没想到老外在中国还可以开车呀 用 API 获取官方统计数据,原来可以这么简单 放弃ipup系列,开始使用更先进的nmcli来管理你的网络 定期启动vpn - db2zos 几段Python小程序 关于职业的一点忧虑和思考 select in 在postgresql的效率问题 Ansible 学习笔记 ldap配置记录 nis,nfs,pam小结 docker命令小记 性能调优利器之strace 如何写出优雅的Python(二) 如何写出优雅的Python 分布式数据库架构一例 如何写出优雅的Python之设置class缺省值 Mac 使用笔记 开启刷题模式 从简单需求到OLAP的RANK系列函数 数据库的Index Scan V.S. Rscan
[LeeCode]Power of Two
db2zos · 2015-07-19 · via 博客园 - db2zos

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

My initial code:

 1 class Solution:
 2     # @param {integer} n
 3     # @return {boolean}
 4     def isPowerOfTwo(self, n):
 5         if n==0 :
 6             return False
 7         if n==1 or n==2:
 8             return True
 9         if n % 2 != 0:
10             return False
11         if n < 4 and n 
12         return self.isPowerOfTwo(n/2)

After google the internet, the best solution is:

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