lc面试准备:Power of Two
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的更多相关文章
- lc面试准备:Remove Duplicates from Sorted List
1 题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- LC 869. Reordered Power of 2
Starting with a positive integer N, we reorder the digits in any order (including the original order ...
- lc面试准备:Implement Stack using Queues
1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...
- lc面试准备:Implement Queue using Stacks
1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...
- 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 ...
- lc面试准备:Repeated DNA Sequences
1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- lc面试准备:Number of 1 Bits
1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- lc面试准备:Reverse Bits
1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
随机推荐
- JDK中DNS缓存的分析
在JAVA中使用InetAddress.getByName(String host) 方法来获取给定hostname的IP地址.为了减少DNS解析的请求次数,提高解析效率,InetAddress中提供 ...
- angularjs ng-repeat checkbox
<div class="col-md-3" ng-repeat="user in title.UserList"> <p class=&quo ...
- 调试php的soapCient
try { import('@.Ext.xml'); header("Content-Type:text/html; charset=utf-8"); $soap = new So ...
- [转载]Access to the path '' is denied.解决方案
原文地址:Access to the path '' is denied.解决方案作者:趴着墙等红杏 ccess to the path '路径' is denied.我在网上找了很多资料,最后终于解 ...
- Gym 100187M-Heaviside Function
题意:给定函数: f(x) = θ(s1x - a1) + θ(s2x - a2) + ... + θ(snx - an), where si = ± 1. Calculate its values ...
- U3D 精灵的点击监听
U3D游戏中,可能会用到点击对象,完成某项操作, 方法一:可以通过接收Input对象的输入,进行利用 方法二:给对象绑定一个collier 组件,然后就能后使用内置方法 这里有点不同,方法一,是不管哪 ...
- 网络解析之XML及JSON
首先要加入类库GDataXMLNode和JSON 解析本地文件Students.txt <students> <student> <name>汤姆 </nam ...
- C#中的面向对象编程
所有的面向对象语言都具有3个基本特征,C#也是不例外的. 封装---把客观事物封装成类,并将类内部的实现隐藏,以保证数据的完整性: 继承---通过继承可以复用父类的对象: 多态---允许将子对象赋值给 ...
- C# 基础 知识点
类型 1.decimal为高精度浮点数,常用于货币计算,然后它不是基本类型,所以性能相对float和double要差. 2.@用于字符串前使转义字符 \ 无效,甚至能将回车当作换行符直接赋值给字符串 ...
- 比较ArrayList和LinkedList
比较一:添加内容 涉及方法:add public void add_test(){ List<Person> addlist = new ArrayList<Person>() ...