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. hdu2602 DP (01背包)

    题意:有一个容量 volume 的背包,有一个个给定体积和价值的骨头,问最多能装价值多少. 经典的 01 背包问题不谈,再不会我就要面壁了. 终于有一道题可以说水过了 ……心好累 #include&l ...

  2. dgraph 数据加载

    dgraph 可以方便的进行大量的数据加载 下载rdf 文件 wget "https://github.com/dgraph-io/tutorial/blob/master/resource ...

  3. PHP安装sqlsrv和memcache扩展步骤

    做了两天的实验才终于摸清楚如何将PHP连接上sqlserver数据库,以及怎样通过修改virtualhost文件来重定向,因为走得弯路比较多所以很想分享一下这次的心路历程. 第一步:安装wamp等类似 ...

  4. MSMQ-发送消息到远程专用队列path格式

    在工作组模式下,远程访问专用队列.在网上找到一篇文章,翻译了一下. 最后结论,直接使用多元素格式名方式,利用IP地址直接对单个或多个目标发送消息      MessageQueue rmQ = new ...

  5. 搭建openwrt_x86虚拟机环境

    1.下载源码 http://downloads.openwrt.org/snapshots/trunk/x86/openwrt-x86-generic-combined-ext4.img.gz 2.格 ...

  6. Django QueryDict

    QueryDict默认是不可变的,同过将QueryDict对象的_mutable 属性的值设置成True就可以为其赋值.QueryDict对象的urlencode()方法将QueryDict转换为字符 ...

  7. 360 杀毒几K每秒的IO读取,SO MAD

    在没有用360杀毒扫描的状态下,从任务管理器中查看,居然有几K每秒的IO读取 . 好卡,直接卸载.

  8. oracle 恢复备份

    select * from dbconninfo update dbconninfo set url = 'jdbc:oracle:thin:@(description=(address_list=( ...

  9. Winfrom 实现转圈等待

    1.放弃进度条.动态进度图片等方式实现用户体验优化方式(主要是优化用户等待体验),建议使用方式? 答:对于From或者Control而言,其提供了Cursor属性设置即可. 例如: this.Curs ...

  10. vue 之radio绑定v-model

    示例: 单选radio <label ><input type="radio" value="0" v-model="branch& ...