LeetCode Power of Four
原题链接在这里:https://leetcode.com/problems/power-of-four/
题目:
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
题解:
与Power of Two, Power of Three类似。
每次iteration若是不能被4整除即return false. 若可以被4整除,便除以4, 直到结果等于1.
Time Complexity: O(log(num)). Space: O(1).
AC Java:
public class Solution {
public boolean isPowerOfFour(int num) {
if(num<=0){
return false;
}
while(num%4 == 0){
num /= 4;
}
return num==1;
}
}
Follow up 需要no loops/recursion.
可以bit manipulation, 首先判定num是否为2的幂数. 若是2的幂数, num二进制表达首位为1, num-1除首位均为1. num & num-1 应等于 0.
然后判定首位的1是在奇数位置上, eg. 10000 = 16. 所以 num & 0x55555555 应等于num 原值.
Time Complexity: O(1). Space: O(1).
public class Solution {
public boolean isPowerOfFour(int num) {
return num>0 && (num & num-1) == 0 && (num & 0x55555555) == num;
}
}
LeetCode Power of Four的更多相关文章
- [LeetCode] Power of Four 判断4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...
- [LeetCode] Power of Three 判断3的次方数
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
- [LeetCode] Power of Two 判断2的次方数
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- LeetCode Power of Three
原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...
- Leetcode Power of Two
Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...
- Leetcode Power of two, three, four
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- leetcode power(x,n)
class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...
- LeetCode——Power of Two
Description: Given an integer, write a function to determine if it is a power of two. public class S ...
- [LeetCode]Power of N
题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...
随机推荐
- AngularJS 表单
AngularJS 表单是输入控件的集合. HTML控件 以下 HTML input 元素被称为 HTML 控件: input 元素 select 元素 button 元素 textarea 元素 H ...
- Pfile VS Spfile (MOS Note 249664.1)
============================================================================== Until Oracle 8i DBAs ...
- jquery 20161014
jquery.fn.extend <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
- sar命令的使用
http://blog.csdn.net/wudiyi815/article/details/7494847 http://blog.csdn.net/colin_liu2009/article/de ...
- Linux学习笔记(4)-远程登录
根据网上的那些说法,如Linux服务器假设在外地(新疆),和程序员工作的环境(北京)相距太远,那么每次出问题都要出差跑到现场去调试的话,那就太烦人了. 所以,人们开发出了一种远程登录的手段,可以让程序 ...
- js添加var和不加var区别
var 声明的变量,作用域是当前 function 没有声明的变量,直接赋值的话, 会自动创建变量 但作用域是全局的. //----------------- function doSth() { a ...
- zorka源码解读之通过beanshell进行插桩的流程
zorka中插桩流程概述 1.在SpyDefinition中配置插桩属性,将SpyDefinition实例提交给插桩引擎.2.SpyDefinition实例中包含了插桩探针probes,probe插入 ...
- 【BZOJ2157】旅游 LCT
模板T,SB的DMoon..其实样例也是中国好样例...一开始不会复制,yangyang:找到“sample input”按住shift,按page down.... #include <ios ...
- Jmeter性能测试 入门
Jmeter是一款优秀的开源测试工具, 是每个资深测试工程师,必须掌握的测试工具,熟练使用Jmeter能大大提高工作效率. 熟练使用Jmeter后, 能用Jmeter搞定的事情,你就不会使用LoadR ...
- word2013删除下载的模板
word2013删除下载的模板 删除步骤: 1)关闭相关的word文档. 2)按照以下的路径找到相应的位置:"%系统根目录%\Users\Administrator\AppData\Roam ...