public class Solution {
public bool IsPowerOfTwo(int n) {
return ((n & (n - )) == && n > );
}
}

https://leetcode.com/problems/power-of-two/#/description

补充python的实现:

 class Solution:
def isPowerOfTwo(self, n: int) -> bool:
return n > 0 and n & (n - 1) == 0

算法思路:位运算。

2 ^ 0 = 1,二进制表示:0000 0001,(1-1)的二进制表示:0000 0000,1 & 0 = 0

2 ^ 1 = 2,二进制表示:0000 0010,(2-1)的二进制表示:0000 0001,2 & 1 = 0

2 ^ 2 = 4,二进制表示:0000 0100,(4-1)的二进制表示:0000 0011,4 & 3 = 0

leetcode231的更多相关文章

  1. [Swift]LeetCode231. 2的幂 | Power of Two

    Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...

  2. leetcode231 2的幂 leetcode342 4的幂 leetcode326 3的幂

    1.2的幂 正确写法: class Solution { public: bool isPowerOfTwo(int n) { ) return false; )) == ; } }; 错误写法1: ...

  3. LeetCode231.2的幂

    231.2的幂 描述 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 示例 1: 输入: 1 输出: true 解释: 2^0 = 1 示例 2: 输入: 16 输出: true 解释 ...

  4. LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four

    位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. ...

  5. LeetCode 231

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

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. (转)Linux 定时关机、休眠命令

    立刻关机:sudo haltsudo init 0 sudo shutdown -h nowsudo shutdown -h 0....定时/延时关机:sudo shutdown -h 19:3019 ...

  2. MySQL Transaction--RC和RR区别

    在MySQL中,事务隔离级别RC(read commit)和RR(repeatable read)两种事务隔离级别基于多版本并发控制MVCC(multi-version concurrency con ...

  3. oracle mysql sql serve where in 语句的不同

    类似这样的语句在mysql  oracle 是可以执行成功的, select * from classfirst where (classid ,classname) not in (select c ...

  4. 调用飞信HTTP接口给自己发短信

    注: 1.下文中所有HTTP请求所指的Host都是f.10086.cn 2.目前只有中国移动用户可以使用 1.打开登录页面:GET /huc/user/space/login.do?m=submit& ...

  5. word如何让单页变横向

    word作为图文排版用户最多的软件之一,其功能的强大自不必说,比如将某一页在版式排版上设置为横排方向.那么,应该如何才能设置为横排的纸张呢?请阅读下文! 工具/原料 Microsoft Office ...

  6. Centos 6.5 yum 安装Apache软件

    首先在系统上面查询一下是否已经安装了apache 软件[Apache软件在linux系统里的名字是httpd] rpm    -qa    httpd 如果有返回的信息,则会显示已经安装的软件.如果没 ...

  7. hdu 1506 Largest Rectangle in a Histogram——笛卡尔树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 关于笛卡尔树的构建:https://www.cnblogs.com/reverymoon/p/952 ...

  8. bzoj 2784 [JLOI2012]时间流逝——树上高斯消元

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2784 一个状态可以加很多个能量圈,但减少能量圈的情况只有一种.所以可以用树来刻画. 然后就变 ...

  9. json包含单双引号问题解决方案

    解决方案:在后台处理 JSONArray.fromObject(list).toString() 转自明明如月小角落: 效果DEMO: JsonQuotesUtil.js /** * 解决json传输 ...

  10. bisect维持已排序的序列

    如下: import bisect # 用来处理已排序的序列,用来维持已排序的序列,升序 # 基于二分查找 li = [] bisect.insort(li, 2) bisect.insort(li, ...