[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 ...
随机推荐
- 学习Microsoft Visio(1)
基础篇 一.认识Visio 1.Visio是什么 Visio最初属于Visio公司,该公司成立于1990年9月.1992年,公司更名为Shapeware.同年11月,它发布了他们公司的第一个产品:Vi ...
- Convert.ToString(null) => null
{ string str0 = Convert.ToString(null); Console.WriteLine("0,{0}", str0); if (str0==" ...
- 性能:Receiver层面
创建多个接收器 多个端口启动多个receiver在其他Executor,接收多个端口数据,在吞吐量上提高其性能.代码上: import org.apache.spark.storage.Storage ...
- django-全文解锁和搜索引擎
安装和配置 全文检索安装 pip install django-haystack==2.5.1 # 2.7.0只支持django1.11以上版本 搜索引擎安装 pip install whoosh 安 ...
- idea中properties配置文件 注释显示中文乱码问题
- nexus 3.17.0 简单试用
老样子,使用docker-compose 运行 环境准备 docker-compose 文件 version: "3" services: nexus: image: sonaty ...
- dinoql 试用
dinoql 前面有过介绍,详细的参考文档即可,这篇主要是简单使用 注意目前dinoql 直接通过node 运行会有window 的问题,有好几种解决方法,后边会说明 环境准备 项目初始化 yarn ...
- 浏览器端使用less
一.思路 ①less无法在浏览器中直接使用,浏览器不能识别 ②通过less解析插件less.js(JavaScript插件)可以把less文件解析成css代码(下载地址) 二.具体应用 ①less.j ...
- MQTT 遗嘱使用
大部分人应该有这个需求: 我想让我的APP或者上位机或者网页一登录的时候获取设备的状态 在线还是离线 设备端只需要这样设置 注意:MQTT本身有遗嘱设置 所以大家可以设置遗嘱 ,注意哈,发布的主题 ...
- 数据结构实验之排序四:寻找大富翁(SDUT 3401)
#include <stdio.h> #include <stdlib.h> #include <string.h> void Swap(int a[], int ...