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 ...
随机推荐
- EF Core中怎么实现自动更新实体的属性值到数据库
我们在开发系统的时候,经常会遇到这种需求数据库表中的行被更新时需要自动更新某些列. 数据库 比如下面的Person表有一列UpdateTime,这列数据要求在行被更新后自动更新为系统的当前时间. Pe ...
- RBG颜色对照表:有网址
RBG颜色对照表 大家都懂的RBG颜色对照表,想做一个有个性和美观的网页,风格是必须要有的,那么多姿多彩的颜色必然是装饰网页的一簇鲜花,为了方便查找比对颜色,就做了这个 网址为: http://too ...
- 基于多用户的Oracle数据泵导入导出数据
登陆SqlPlus: SqlPlus sys/syspwd@MyOrcl AS sysdba 其中:syspwd:sys的登陆密码:MyOrcl:所创建的数据库服务名. 创建数据泵: create o ...
- git 码云 使用记录
使用了码云的私有仓库. 一.首先下载安装git 安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功! 二.创建版本库 什么是版本库 ...
- ubuntu8.04下mysql更改用户和密码
1.最近由于系统原因重装了mysql,但是发现安装过程中没有提示设置密码. 2.修改用户名和密码步骤 A.service mysql stop #停止mysql服务 B.sudo vim /et ...
- Dubbo 安装ZooKeeper环境
一.在Windows 安装ZooKeeper 1.下载ZooKeeper 2.解压,修改ZooKeeper配置文件 复制一份zoo_sample.cfg文件,改名位zoo.cfg,打开编辑,设置数据保 ...
- 新手Linux命令学习
一.dd命令:1.可以复制文件,2.可以制作ios镜像,简单理解就是备份 常用的参数 if 设置输入文件的名称 of 设置输出文件的名称 bs 设置每个“”块“”大小 count 要复制“块” ...
- 在java程序中使用JDBC连接mysql数据库
在java程序中我们时常会用到数据库中的数据或操作数据库中的数据,如果java程序没有和我们得数据库连接,就不能实现在java程序中直接操作数据库.使用jdbc就能将java程序和数据库连起来,此时我 ...
- 关于vue中mockjs的使用
使用vue的时候,后台可能不能及时作出接口,那么就需要我们前端自己模拟数据,使用mockjs可以进行模拟数据. 首先安装mockjs,cnpm install mockjs --save-dev: 其 ...
- linux系统快速安装宝塔
宝塔面板分linux面板和windows面板,安装宝塔linux面板首先要访问宝塔官网查看对应版本进行选择 宝塔面板的安装需要注意的地方有: 1.纯净系统 2.确保是干净的操作系统,没有安装过其它环境 ...