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. VC++ 获取windows系统的版本类型

    vc中获取windows版本信息,一般是调用GetVersionEx 这个API函数来获取的,这个API需要OSVERSIONINFOEX 这个结构体作为参数,OSVERSIONINFOEX 的对应的 ...

  2. System Operations on AWS - Lab 3W - Managing Storage (Windows)

    创建一个名叫Processor的EC2实例,登陆到CommandHost实例,通过AWS CLI对Processor实例的EBS卷做snapshot,设置周期性snapshot的计划任务, 登陆到Pr ...

  3. linux64下安装swftools

    在文档转换器中,需要在linux上安装swftools,经历了一番曲折过程终于安装成功.swftools安装包从http://www.swftools.org/download.html上面下载. 在 ...

  4. html响应式布局,css响应式布局,响应式布局入门

    html响应式布局,css响应式布局,响应式布局入门 >>>>>>>>>>>>>>>>>>& ...

  5. JDK自带方法实现RSA非对称加密

    package jdbc.pro.lin; import java.security.InvalidKeyException; import java.security.Key; import jav ...

  6. 经常使用的两个清爽的table样式

    两个我经常使用的table样式: <html> <head> <title></title> <style type="text/css ...

  7. 如何用js检测判断时间日期的间距

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

  8. css3遇到的一些属性

    rgba          是由red.green.blue 三种颜色搭配出来的box-shadow     向元素添加阴影层,水平阴影位置,垂直阴影位置,后面是可选:模糊距离,阴影大小,颜色,是否是 ...

  9. PHP验证码的制作

    <?phpsession_start();   //??session//?建随机?,并保存在session中for($i=0;$i<4;$i++){$_nmsg.=dechex(mt_r ...

  10. Java多线程读书笔记之一

    今天开始陆续将这几天跟进Java多线程知识的成果记录下来分享. Java多线程的知识是一直想要系统彻底的看完的,但是懒惰加无聊早就了我每天都没有进展,这回下决心一定要把这块知识系统梳理完. 我的知识来 ...