Description

Using O(1) time to check whether an integer n is a power of 2.

Example

For n=4, return true;

For n=5, return false;

Challenge

O(1) time

题解:题目要求判断一个数是不是2的幂次方,并且规定时间复杂度为线性级。那么,就不能用常规的循环求解思路去解题了,之前做过一个不用加号求两数之和的题目。这两个题目有点类似,可以通过位运算来快速求解。先考虑这样的一个问题:如果一个数是2的幂次方,会有什么特点?计算机中的数都是以二进制保存在计算机中的。下面我们来举几个例子;

十进制                                         二进制

2                                                  10

4                                                   100

8                                                   1000

16                                                  10000

。。。。。。

很直观,2的幂次方有个特点:只有一位是1,其余的都是0.那么怎么利用这个性质呢?有个巧妙的方法:

n                                                     n-1

10                                                   01

100                                                 011

1000                                               0111

10000                                             01111

。。。。。。

会发现,n-1和n每一位都不相同,如果做与(&)运算,那么结果应该为0,这样,这个问题就可以解决了。代码如下:

public class Solution {
/**
* @param n: An integer
* @return: True or false
*/
public boolean checkPowerOf2(int n) {
// write your code here
if(n<=0)
return false;
return ((n&(n-1))==0)? true:false;
}
}

142. O(1) Check Power of 2【LintCode by java】的更多相关文章

  1. 142. O(1) Check Power of 2【easy】

    142. O(1) Check Power of 2[easy] Using O(1) time to check whether an integer n is a power of 2. Have ...

  2. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  3. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  4. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  5. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  6. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  7. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  8. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  9. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

随机推荐

  1. 【luogu P2296 寻找道路】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2296 题意:给定起点终点,找一条从起点到终点的最短路径使路上的每个点都能有路径到达终点. 我们先反着建一遍图 ...

  2. OC变量命名禁忌

    OC变量命名禁忌 1.在NSString类不能定义变量名为description, 每个类都有 - (NSString *)description{} 这样一个get方法. 2.不能将变量的名称定义为 ...

  3. JAVA正则表达式判断元音

    /* * 判断字符串”qaq”中间的字符是否是元音 * * aeiou * AEIOU * */ (1)正则表达式 (2) (3)

  4. 6.可见性关键字(volidate)

    可见性关键字(volidate): 如果对java内存模型了解较清楚的话,我们知道每个线程都会被分配一个线程栈. 线程栈里存的是对象的引用,但当前cache缓存机制,可能会把数据拷贝. 就是,命中缓存 ...

  5. mdev自动创建和删除设备节点

    设备节点的创建有二种方法: 1)手动创建:mknode命令  当insmod后,还需要手动moknod创建设备节点才能被应用层打开,并且使用完成之后还要删除节点. 2) 自动创建:mdev mdev, ...

  6. ajax 与 axios区别

    Ajax: Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发技术. Ajax = 异步 J ...

  7. go语言学习(一):数组/切片

    学习区块链之后,发现还要学习加密,接触到的视频是:Go的crypto包,所以开始接触Go.因为和solidity有些相似,但是比solidity简单一些,就开始放松的心态去学习.每天翻着go菜鸟教程, ...

  8. .Net Core如何在程序的任意位置使用和注入服务

    最近有人问我:我该如何在Startup类之外的地方注入我的服务呢,都写在startup里看着好乱:我该如何在程序的其他地方获取我注入的服务呢: 故我写了这篇博客,文中有不对的地方欢迎指正. 一.如何在 ...

  9. windows下MySQL免安装版配置教程mysql-8.0.12-winx64.zip版本

    引用1:https://blog.csdn.net/weixin_42831477/article/details/81589325 引用2:https://blog.csdn.net/qq_3193 ...

  10. mongodb rebo 3T 执行出错 failed to execute script 但是执行成功 171条

    我现在也不清楚到底是什么原因 解决方法: 把你要执行的脚本保存到文件 在最上面添加下面两行代码:根据你的数据库 信息填写 conn = new Mongo('host:port'); db = con ...