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. 51Nod - 1205 (流水先调度)超级经典的贪心 模板题

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1205 N个作业{1,2,…,n}要在由2台机器M1和M2组成 ...

  2. 使用canvas输出base64_url

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 环境配置之 Debug 和 Release - iOS

    便于开发.打包中在不同环境(测试.生产)间属性的切换更加方便便捷流畅,故创建设置此方式方法,希望对大家能有所帮助. 首先,创建 Configurations Setting File(.xcconfi ...

  4. easyui datagrid 异步加载数据时滚动条有时会自动滚到最底部的问题

    在使用easyui 的datagrid异步加载数据时发现滚动条有时会自动滚到最底部.经测试发现,如果加载数据前没有选中行则不会出现这个问题.这样我们可以在重新异步加载数据前取消选中行就可以避免这个问题 ...

  5. jQuery基本toggle() toggleClass() 使用

    今天来学习一下jQuery的基本函数的使用,很简单. 首先写一个button做控制按钮,然后写一个div用按钮控制idv做动画,从而测试JQuery的动画函数 <head> <met ...

  6. linux操作系统的目录以及用户权权限的管理

    linux操作系统的目录以及对目录的操作 一: linux操作系统的目录结构   bin #可执行程序的安装目录 , 命令 boot #系统启动引导目录 dev #设备目录 etc #软件配置文件目录 ...

  7. php 算法(二分法)只适用于有序表,且限于顺序存储结构

    function demo($array,$low,$high,$k){ if($low<=$high){//判断该数组是否存在 $mid =  intval(($low+$high)/2 ); ...

  8. laravel4.2 Redis 使用

    laravel4.2 Redis 使用 配置文件,app/config/database.php 'redis' => array( 'cluster' => false, 'defaul ...

  9. python应用:爬虫框架Scrapy系统学习第一篇——xpath详解

    HTML的三大概念:标签.元素以及属性 标签:尖括号中的文本       例:<head>……</head> 标签通常成对出现 元素:标签中的所有内容        元素中可包 ...

  10. python爬xx图代码

    今日 好热,照样是挖洞挖不到,看了几天的python爬虫,学会了xpath解析 撸一个代码玩玩] 不要说什么,优化之类的,刚学完,跑了一阵 ,还可以  挺稳定 # -*- coding:utf-8 - ...