1 题目

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

接口 boolean isPowerOfTwo(int n)

2 思路

判断一个整数是否是2的幂次结果数。

0100 ==> 4 ; 1000 ==>8 ; 10000 ==> 16

0011 ==> 3 ; 0111 ==>7 ; 01111 ==> 15

思路1:2的次方数都只有一个1,剩下的都是0。我们只要每次判断最低位是否为1,然后向右移位,最后统计1的个数即可判断是否是2的次方数

复杂度:Time O(32); Space O(1)

思路2:如果一个数是2的次方数的话,那么它的二进数必然是最高位为1,其它都为0,那么如果此时我们减1的话,则最高位会降一位,其余为0的位现在都为变为1,那么我们把两数相与,就会得到0。

复杂度:Time O(1); Space O(1)

3 代码

  • 思路1
        public boolean isPowerOfTwo(int n) {
int count1 = 0;
for (; n > 0;) {
int tmp = n & 1;
count1 += tmp;
n = n >> 1;
}
boolean is = (count1 == 1);
return is;
}
  • 思路2
        public boolean isPowerOfTwo(int n) {
boolean is = false;
if (n > 0) {
is = ((n - 1) & n) == 0;
}
return is;
}

4 总结

这是位操作的智力题。

5 参考

lc面试准备:Power of Two的更多相关文章

  1. lc面试准备:Remove Duplicates from Sorted List

    1 题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  2. LC 869. Reordered Power of 2

    Starting with a positive integer N, we reorder the digits in any order (including the original order ...

  3. lc面试准备:Implement Stack using Queues

    1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...

  4. lc面试准备:Implement Queue using Stacks

    1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...

  5. lc面试准备:Invert Binary Tree

    1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...

  6. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  7. lc面试准备:Number of 1 Bits

    1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  8. lc面试准备:Reverse Bits

    1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  9. lc面试准备:Regular Expression Matching

    1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

随机推荐

  1. 在后台CS文件里面,隐藏和显示Repeater里面控件

    <asp:Repeater ID="Repeater1" runat="server"><ItemTemplate><asp:Pa ...

  2. HTTP 错误 500.19- Internal Server Error 错误解决方法 分类: Windows服务器配置 2015-01-08 20:16 131人阅读 评论(0) 收藏

    1.第一种情况如下: 解决方法如下: 经过检查发现是由于先安装Framework组件,后安装iis的缘故,只需重新注册下Framework就可以了,具体步骤如下 1 打开运行,输入cmd进入到命令提示 ...

  3. PL/SQL 访问网页(get or post方式)

    在我们开发plsql程序的过程中,有时候难免要访问一些外部网站的数据.这个时候我们就要用到utl_http包. 使用utl_http包前需要注意的是,当前的用户下是否有访问外部网络的权限. 如下是自己 ...

  4. java实现时间的比较

    时间大小的比较以及把String类型的时间转换为Date类是时间在开发中是非常常见的,下面的主要是一个工具方法 public class Test { public static void main( ...

  5. 设置透明navigationBar

    三行代码轻松实现透明navigationBar:  [self.navigationController.navigationBar setBackgroundImage:[UIImage new] ...

  6. C# CRC校验的一点感悟

    今天在鼓捣一个手持操作器的时候,遇到一点问题,记录一下今天的经验包 由于之前公司产品在校验时基本上都是和校验,今天在准备用C#模拟一个古董操作器的时候,却遇到一个问题,模拟器发出的数据,主板一律不回复 ...

  7. 8种CSS清除浮动的方法优缺点分析

    为什么清除CSS浮动这么难? 因为浮动会使当前标签产生向上浮的效果,同时会影响到前后标签.父级标签的位置及 width height 属性.而且同样的代码,在各种浏览器中显示效果也有可能不相同,这样让 ...

  8. tomcat gc问题总结

    Java内存泄露监控工具:JVM监控工具介绍  http://developer.51cto.com/art/201203/321431.htm 关于施用full gc频繁的分析及解决  http:/ ...

  9. float浮动引起的ul高度崩溃与overflow的关系

        今天遇到的问题真的让人不得不吐槽,因为一个很小的问题,花费了半天的时间来才解决这个问题.一直认为自己对Html与Css了解应该算蛮不错的,但是今天遇到的事情让我不得不反省自己的学习心态上的错误 ...

  10. javascript图片预先加载

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...