[LeetCode] 342. Power of Four 4的次方数
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?
Credits:
Special thanks to @yukuairoyfor adding this problem and creating all test cases.
给一个有符号的32位整数,写一个函数检查此数是否为4的次方数。
解法1:位操作
解法2:循环
解法3: 数学函数, 换底公式
Java:
public boolean isPowerOfFour(int num) {
int count0=0;
int count1=0; while(num>0){
if((num&1)==1){
count1++;
}else{
count0++;
} num>>=1;
} return count1==1 && (count0%2==0);
}
Java:
public boolean isPowerOfFour(int num) {
while(num>0){
if(num==1){
return true;
} if(num%4!=0){
return false;
}else{
num=num/4;
}
} return false;
}
Java:
public boolean isPowerOfFour(int num) {
if(num==0) return false; int pow = (int) (Math.log(num) / Math.log(4));
if(num==Math.pow(4, pow)){
return true;
}else{
return false;
}
}
Python:
class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
return num > 0 and (num & (num - 1)) == 0 and \
((num & 0b01010101010101010101010101010101) == num)
Python:
# Time: O(1)
# Space: O(1)
class Solution2(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
while num and not (num & 0b11):
num >>= 2
return (num == 1)
C++:
class Solution {
public:
bool isPowerOfFour(int num) {
while (num && (num % 4 == 0)) {
num /= 4;
}
return num == 1;
}
};
C++:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && int(log10(num) / log10(4)) - log10(num) / log10(4) == 0;
}
};
C++:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && !(num & (num - 1)) && (num & 0x55555555) == num;
}
};
C++:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && !(num & (num - 1)) && (num - 1) % 3 == 0;
}
};
类似题目:
[LeetCode] 231. Power of Two 2的次方数
[LeetCode] 326. Power of Three 3的次方数
All LeetCode Questions List 题目汇总
[LeetCode] 342. Power of Four 4的次方数的更多相关文章
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- [LeetCode] 326. 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 342. Power of Four (4的次方)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- LeetCode 342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- Leetcode 342 Power of Four 数论
题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...
- Python [Leetcode 342]Power of Four
题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...
- [LeetCode] 342. Power of Four(位操作)
传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- [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 ...
随机推荐
- Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation(知识图谱)
知识图谱(Knowledge Graph,KG)可以理解成一个知识库,用来存储实体与实体之间的关系.知识图谱可以为机器学习算法提供更多的信息,帮助模型更好地完成任务. 在推荐算法中融入电影的知识图谱, ...
- onreadystatechange和onload区别分析
onreadystatechange和onload区别分析 script加载 IE的script 元素只支持onreadystatechange事件,不支持onload事件. FireFox,Op ...
- 浅谈C++编译原理 ------ C++编译器与链接器工作原理
原文:https://blog.csdn.net/zyh821351004/article/details/46425823 第一篇: 首先是预编译,这一步可以粗略的认为只做了一件事情,那就 ...
- Jupyter notebook 自动补全
Jupyter notebook 自动补全 Jupyter notebook使用默认的自动补全是关掉的.要打开自动补全,需修改默认配置. ipython profile create 以上命令会 ...
- 2019-2020-1 20199302《Linux内核原理与分析》第十一周作业
缓冲区溢出 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器和返回地址的暂时关闭,溢 ...
- chef test-kitchen Could not load the 'vagrant' driver from the load path 问题解决
今天使用chef 的kitchen,运行kitchen list 发现了如下错误: >>>>>> ------Exception------- >>&g ...
- callbag js callback 标准-支持轻量级观测以及迭代
callbag 是一个js 的回调标准,方便开发支持观测以及迭代的代码 类似已经有好多的实现了 callbag-basics 比rxjs 以及xstream 还快 wonka 说明 基于标准的开发,对 ...
- PCA与ICA
关于机器学习理论方面的研究,最好阅读英文原版的学术论文.PCA主要作用是数据降维,而ICA主要作用是盲信号分离.在讲述理论依据之前,先思考以下几个问题:真实的数据训练总是存在以下几个问题: ①特征冗余 ...
- vuex如何实现数据持久化,刷新页面存储的值还存在
1.安装: npm install vuex-persistedstate --save 2.找到store/index.js import Vue from 'vue' import Vuex fr ...
- 「ZJOI2019」语言
传送门 Description 给定一棵\(n\)个点的树和\(m\)条链,两个点可以联会当且仅当它们同在某一条链上,求可以联会的点的方案数 \(n,m\leq10^5\) Solution 考虑计 ...