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. Java基础知识强化之网络编程笔记05:UDP之多线程实现聊天室案例

    1. 通过多线程改进刚才的聊天程序,这样我就可以实现在一个窗口发送和接收数据了 2.  代码示例: (1)SendThread.java,如下: package com.himi.udpDemo2; ...

  2. [转载]js中__proto__和prototype的区别和关系

          首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性_ ...

  3. CentOS7系统下搭建Jenkins环境

    1. 安装JDK yum -y install java 2.安装Ant 添加JPackage源 yum -y install wget wget http://www.jpackage.org/jp ...

  4. [XML] Resource帮助类

    点击下载 Resources.rar /// <summary> /// 类说明:Resources /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[u ...

  5. python查看网站的RTT

    import requests time=0.0 jpserver=['jp1.herejump.com','jp1.herejump.com','jp1.herejump.com'] usserve ...

  6. Sql解锁 数据库死锁检测

    USE [DataBaseName]GO/****** Object:  StoredProcedure [dbo].[sp_check_deadlock]    Script Date: 07/04 ...

  7. js基础知识之_对象

    javascript 对象 1.基于对象 一切皆对象,以对象的概念来编程 2.面向对象的编程(oop,Object oriented programming) 1.对象 就是人们要研究的任何事物,不仅 ...

  8. jQuery AJAX load() 方法

    jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法: $(selector). ...

  9. Memento 模式

    Memento 模式的关键就是要在不破坏封装行的前提下,捕获并保存一个类的内部状态,这样就可以利用该保存的状态实施恢复操作. /////////Originator.h//////////////// ...

  10. leetcode中一些要用到动态规划的题目

    需要仔细回顾的题目: 1.Interleaving String   交叉存取字符串 2.Decode Ways   字符串解码 3.Subsets   Subsets II          求一个 ...