142. O(1) Check Power of 2【LintCode by java】
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】的更多相关文章
- 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 ...
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 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 ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
- 30. Insert Interval【LintCode by java】
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
- 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 ...
- 211. String Permutation【LintCode by java】
Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...
随机推荐
- 大数据框架-Mapreduce过程
1.Shuffle [从mapTask到reduceTask: Mapper -> Partitioner ->Combiner -> Sort ->Reducer] mapp ...
- meta的随堂笔记
Meta标签与搜索引擎优化(SEO) 概要 通常所说的meta标签,是在Html网页源代码中的一个重要的html标签.meta标签用来描述一个html网页文档的属性,例如作者.日期和时间.网页描述.关 ...
- java判断类型
判断是否String:str.getClass().getName().equals("java.lang.String") 判断是否在且不为空:Object.hasKey(&qu ...
- hashMap 和 linkedHashMap 的区别和联系
直接举例说明. 运行如下例子程序 mport java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; ...
- js检测是否可以访问公网服务器
wifi认证开发过程所用到的,源码如下: 注:检测AC是否放行成功,是否可以访问公网阿里云服务器 功能调用: checkNet().then(function(res) { if(res) { //连 ...
- 19-3-6Python中字典的解释、使用、嵌套
一.字典 为什么学字典: 列表的缺点: 1.列表如果存储的数据比较多,那么他的查询速度相对慢. 2.列表存储的数据关联性不强. 字典是什么: Python基础数据类型之一:字典. Python中唯一的 ...
- (Nagios)-check_hpasm[HP]
Nagios Check_hp HP 2014年11月18日 下午 08:49 https://IP:2381 [root@nagios ~]# tar zxvf check_hp_blad ...
- Centos配置静态IP
ifconfig -a //看IP,HWADDR netstat -rn //看网关 service network restart //重启网卡 输入命令:vi ...
- mysql8.0新增用户及密码加密规则修改
MySQL8.0已经发布GA版,当前最新GA版本为8.0.12.虽然相对于之前版本,MySQL8.0没有加入新元素,但是,经过代码重构,MySQL8.0的优化器更加强大,同时也有一些新特性,如支持索引 ...
- C#的哈希表Hashtable同步方法
在多线程环境的操作中对Hashtable进行操作需要进行同步控制,有两种方法,一种是由.Net自动控制:一种是在代码中自己控制. 1.使用Hashtable.Synchronized进行同步 Hash ...