342 Power of Four 4的幂
给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂。
示例:
当 num = 16 时 ,返回 true 。 当 num = 5时,返回 false。
问题进阶:你能不使用循环/递归来解决这个问题吗?
详见:https://leetcode.com/problems/power-of-four/description/
C++:
方法一:
class Solution {
public:
bool isPowerOfFour(int num) {
while(num&&(num%4==0))
{
num/=4;
}
return num==1;
}
};
方法二:
class Solution {
public:
bool isPowerOfFour(int num) {
return num>0&&!(num&num-1)&&(num-1)%3==0;
}
};
参考:https://www.cnblogs.com/grandyang/p/5403783.html
342 Power of Four 4的幂的更多相关文章
- 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂
231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- 342. Power of Four(One-line)
342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...
- 342. Power of Four
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
- [LeetCode] 342. Power of Four(位操作)
传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...
- 【LeetCode】342. Power of Four 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 位运算 函数法 日期 [LeetCode ...
- 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的幂数. ...
随机推荐
- 团队开发git使用各种问题
参考:https://www.cnblogs.com/schaepher/p/4933873.html 问题-3:保持github上项目干净,对于在不同机器上运行会不同的文件不予维护(如.idea/w ...
- HDU 4903 (模拟+贪心)
Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a he ...
- 回文质数 USACO
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 15 ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
- 洛谷—— P2330 [SCOI2005]繁忙的都市
P2330 [SCOI2005]繁忙的都市 题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路 ...
- NETCore项目报错 An error occurred while starting the application
在发布到IIS的webApi项目中,运行时报出以上错误, 解决方法: 1.打开发布目录文件夹,找到web.config文件 2.打开web.config找到stdoutLogEnabled=" ...
- Linux中W与Who命令的使用
踢掉一个从某个终端连上的用户pkill -kill -t pts/0 ---------------------------------------------------------------- ...
- 从理论到实践,全方位认识DNS(理论篇)
对于 DNS(Domain Name System) 大家肯定不陌生,不就是用来将一个网站的域名转换为对应的IP吗.当我们发现可以上QQ但不能浏览网页时,我们会想到可能是域名服务器挂掉了:当我们用别人 ...
- 错误代码: 1045 Access denied for user 'skyusers'@'%' (using password: YES)
1. 错误描写叙述 GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "."; 1 queries e ...
- 转 BlockingQueue(阻塞队列)详解
转自 http://wsmajunfeng.iteye.com/blog/1629354 前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输” ...