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. JQuery实现高级检索功能

    https://blog.csdn.net/muziruoyi/article/details/44494465 < div id= "0" class ="row ...

  2. 红黑树-算法大神的博客-以及java多线程酷炫的知识

    http://www.cnblogs.com/skywang12345/p/3245399.html 解释第5条:从 ->根节点(或者任意个结点)到->所有的末端节点的路径中 ->黑 ...

  3. day31 python学习 并发编程之多进程理论部分

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): 二 进程与程序的区别 程序仅仅只是一堆代码而已,而进程指的是程序的运行 ...

  4. 用vmware安装gho文件心得

    在卡饭学到了不少知识,下面是我的一个心得分享,希望大家能用的上. 用vmware安装gho文件心得 方法1:diskgenius+ghostexp用vm新建一个空白硬盘虚拟机, 记住虚拟机文件的存储位 ...

  5. 使用 Python 连接到 PADS Layout

    使用 Python 连接到 PADS Layout PADS Layout 使用的是 VBA 编程,很多人说 VBA 很简单,但是实在学不会,可能是太笨了. 后来发现 PADS Layout 有 CO ...

  6. php 中的引用

    php 有类似 C 中的指针 &. 但在 php 中叫 引用. 虽然和 传地址很像,但是差别很大.(估计底层实现应该差不多,只是猜想,有机会再研究) 这里有一个关于 php 的对象的赋值其实就 ...

  7. centos 修改host

    centos 修改host vi /etc/hosts添加一行127.0.0.1 test.com /etc/rc.d/init.d/network restart

  8. jmeter ---模拟http请求/发送gzip数据

    jmeter中get请求gzip数据的方法: 在jmeter线程组中添加“http信息头管理器”,并添加名称:Accept-Encoding值: gzip,deflate注:HTTP信息头Accept ...

  9. C# 日期格式化的中的(/)正斜杠的问题(与操作系统设置有关)

    Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd" )); //这行代码, 如果你在系统日期格式默认的情况下输出 2013/0 ...

  10. bzoj 4842: [Neerc2016]Delight for a Cat

    Description ls是一个特别堕落的小朋友,对于n个连续的小时,他将要么睡觉要么打隔膜,一个小时内他不能既睡觉也打隔膜 ,因此一个小时内他只能选择睡觉或者打隔膜,当然他也必须选择睡觉或打隔膜, ...