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. 题意:判断一个数是否是 ...
随机推荐
- php页面编码与字符操作
我们可以用header来定义一个php页面为utf编码或GBK编码,也可以在html中用meta标签来指定编码 例如:php页面为utf编码 header("Content-type: ...
- [C++][数据结构]队列(queue)的实现
对于队列的定义,前人之述备矣. 队列的实现方法与栈非常相似.我直接在我实现的那个栈的代码上加了一点东西,全局替换了一些标识符,就实现了这个队列. 我实现的是一个queue<value>容器 ...
- hdu 5073
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 思路:一开始忘了排序,wa了好几发...选择区间长度为N - K的连续的数, 然后其余的K个数都 ...
- [原创]Centos7 内部常用软件升级计划
GCC 当前系统版本 gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
- Android取得图库图片的具体地址
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResu ...
- 《DSP using MATLAB》示例Example5.14
代码: x1 = [1,2,2]; x2 = [1,2,3,4]; y = circonvt(x1,x2,4) n1 = 0:1:length(x1)-1; n2 = 0:1:length(x2)-1 ...
- 10 Cookie/Session
JSP/EL入门 * SUN提供了开发WEB资源的技术 Servlet/JSP * response.getWriter().write(); ...
- self.automaticallyAdjustsScrollViewInsets
导航视图内Push进来的以“TableView”(没有ScrollView截图,就将就一下)为主View的视图,本来我们的cell是放在(0,0)的位置上的,但是考虑到导航栏.状态栏会挡住后面的主视图 ...
- OS X El Capitan的 U 盘制作过程
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 0.在 AppStore 下载 OS X El Capitan AppStore ...
- 【BZOJ】3922: Karin的弹幕
题意 给定一个长度为\(n(1 \le n \le 70000)\)序列,\(m(1 \le m \le 70000)\)次操作:1. 对一段下标是等差数列的子序列求最大值:2. 单点修改. 分析 如 ...