【一天一道LeetCode】#342. Power of Four
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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?
(二)解题
题目大意:判断一个数是不是4的n次方数。
解题思路:可以参考:【一天一道LeetCode】#326. Power of Three和【一天一道LeetCode】#231. Power of Two
首先,最简单的方法,采用循环来做:
class Solution {
public:
bool isPowerOfFour(int num) {
int n = num;
while(n>0&&n%4==0)
{
n/=4;
}
return n==1;
}
};
题目中有提问说能不能不用循环和递归来做,和判断3的n次方数一样,4的n次方数在整形数内只有16个,很快就能列举出来,也不失为一个好办法。
另外观察4的n次方数和2的n次方数,可以看出,2的n次方数中能开方得到整数的均为4的n次方数。
class Solution {
public:
//三个条件:大于0,是2的n次方数,开方后得到整数
bool isPowerOfFour(int num) {
return (num>0)&&!(num&(num-1))&&(sqrt(num)*sqrt(num)==num);
}
};
在leetcode的讨论区看到这样一个答案,很巧妙。
首先判断num是不是2的n次方数,在判断他与0xaaaaaaaa相与是否为0。
4的n次方数有一个特点就是,在bit位上,第0,2,4,6,8,10….位上为1,
所以,先判断是不是3的n次方数,就保证了有且仅有一位上为1的数,然后在剔除奇数位上为1的数,剩下的就是4的n次方数了。
class Solution {
public:
bool isPowerOfFour(int num) {
//三个条件:大于0,是2的n次方数,有且仅有偶数位上为1
return (num>0)&&!(num&(num-1))&&!(num&0xaaaaaaaa);
}
};
【一天一道LeetCode】#342. Power of Four的更多相关文章
- [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 (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】#231. Power of Two
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#326. Power of Three
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Python【第四课】 装饰器
本篇内容 什么是装饰器 装饰器需要遵循的原则 实现装饰器的知识储备 高阶函数 函数嵌套 闭包函数 无参函数 装饰器示例 1.什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本 ...
- Mysql--存储引擎(MyISam & InnoDB)
Mysql 系列文章主页 =============== 查看 Mysql 支持的存储引擎: show engines; 查看当前数据库使用的存储引擎: show variables like '%s ...
- form submit提交
form内控件参数自动添加到url后,而自定义的url参数则不能添加到url后 $('#fm').form('submit', { url: 'Data/Diary.ashx?dt=' + new D ...
- javascript装饰器模式
装饰器模式 什么是装饰器 原名decorator 被翻译为装饰器 可以理解为装饰 修饰 包装等意 现实中的作用 一间房子通过装饰可以变得更华丽,功能更多 类似一部手机可以单独使用 但是很多人都愿意家个 ...
- Python小代码_9_求水仙花数
for i in range(100, 1000): ge = i % 10 shi = i // 10 % 10 bai = i // 100 if ge ** 3 + shi ** 3 + bai ...
- 16. 3Sum Closest(中等)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Lintcode389 Valid Sudoku solution 题解
[题目描述] Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty ...
- jenkins + pipeline构建自动化部署
一.引言 Jenkins 2.x的精髓是Pipeline as Code,那为什么要用Pipeline呢?jenkins1.0也能实现自动化构建,但Pipeline能够将以前project中的配置信息 ...
- 【python进阶】Garbage collection垃圾回收2
前言 在上一篇文章[python进阶]Garbage collection垃圾回收1,我们讲述了Garbage collection(GC垃圾回收),画说Ruby与Python垃圾回收,Python中 ...
- 反射 类的加载 Schema DOM 解析方式和解析器 命名空间
Day15 反射 1.1 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. l 加载 就是指将class文件读入内存,并为之创建 ...