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. 题意:判断一个数是否是 ...
随机推荐
- hdu 1503 Advanced Fruits
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 思路:这是一道最长公共子序列的题目,当然还需要记录路径.把两个字符串的最长公共字串记录下来,在递 ...
- 11g新特性-dba_users安全性的一些增强
1.dba_user表的password(除了GLOBAL和EXTERNAL的密码)不再保存密码. 查询10g的dba_user表 SQL> select username,password f ...
- 在SQL SERVER中实现RSA加解密函数(第二版)
/*************************************************** 作者:herowang(让你望见影子的墙) 日期:2010.1.5 注: 转载请保留此信息 更 ...
- IntelliJ IDEA 14 注册码
IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7 ...
- 如何转换WMV到MP3,WMV到MP3播放器
非常好的软件!!!!!没有注册,可以用.推荐给大家! http://www.daniusoft.com/cn/convert-wmv/wmv-to-mp3.html http://hi.baidu.c ...
- 手动编译安装docker环境,以及偶尔出现的bug
总结安装过程如下: 前提:安装git,go,make, docker(docker中编译docker) git clone https://git@github.com/docker/docker c ...
- Linux UBI子系统设计初探
问题领域 flash存储设备存在如下特点: 存在坏块 使用寿命较短 存储介质不稳定 读写速度慢 不支持随机访问(nand) 只能通过擦除将0改成1 最小读写单位为page or sub-page 便宜 ...
- *HDU1800字典树
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- VerifyCodeUtil.java
package com.vcredit.framework.utils; import java.awt.Color;import java.awt.Font;import java.awt.Grap ...
- Oracle EBS Setup
1. Prevent close other forms after close original form