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 ...
随机推荐
- opencv多平台环境搭建及使用
windows平台: 一.安装opencv 下载地址:http://opencv.org/ 依据平台下载相应源码包 安装流程就是一个解压过程.不再赘述. 解压完,效果图: 源码树结构参看http:// ...
- javascript异步加载详解(转)
本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading),延迟加载(lazy loading),延迟执行(lazy execution),async 属 ...
- 最近有机会接触到了angularJs
记点笔记 概念多了 理顺还待时日: 总的来说: 1.ng-src src属性 2.ng-href href属性 3.ng-checked 选中状态 4.ng-selected 被选择状态 5.ng- ...
- 学习java随笔第八篇:封装、继承、多态
java和c#一样都是面向对象的语言. 面向对象的语言有三大特征:封装.继承.多态 封装 封装:隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别. class Perso ...
- Windows下的多线程
Windows下的进程和Linux下的进程是不一样的,它比较懒惰,从来不执行任何东西,它只是为线程提供执行环境,然后由线程负责执行包含在进程的地址空间中的代码.当创建一个进程的时候,操作系统会自动创建 ...
- error: Unable to find vcvarsall.bat while install python library by pip install or python setup.py install.
Python 2.7 会搜索 Visual Studio 2008. 如果你电脑上没有这个版本的话,比如只有: 1.Visual Studio 2010,在cmd里面执行:SET VS90COMNTO ...
- java基础部分
1.java的基本数据类型及所占的字节 boolen 8位 1个字节 byte 8位 1个字节 char 16位 2个字节 short 16位 2个字节 int 32位 4个字 float 32位 ...
- C#.NET Winform 快速开发平台
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 适用软件:适合开 ...
- 安装python-MySQLdb 出现error: command 'gcc' failed with exit status 1的解决方法
>>> yum install MySQL-p* >>>yum install python-devel >>>cd MySQL-python-1 ...
- iOS: 学习笔记, 用代码驱动自动布局实例
iOS自动布局是设置iOS界面的利器. 本实例展示了如何使用自动布局语言设置水平布局, 垂直布局 1. 创建空白iOS项目 2. 添加一个控制器类, 修改YYAppDelegate.m文件 #impo ...