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的幂数. ...
随机推荐
- RAII手法封装互斥锁
RAII手法是 Resource Acquisition is Initialization 的缩写,意为“资源获取即初始化”,在使用智能指针时也使用,下面是针对互斥量时的实现, #include & ...
- angular中多个promise的合并处理
all()方法 这个all()方法,可以把多个primise的数组合并成一个.当所有的promise执行成功后,会执行后面的回调.回调中的参数,是每个promise执行的结果.当批量的执行某些方法时, ...
- [luoguP1005] 矩阵取数游戏(DP + 高精度)
传送门 和奶牛那个题很像,每一行状态互不影响,也就是求 n 遍DP 不过高精度非常恶心,第一次写,调了我一上午. ——代码 #include <cstdio> #include <c ...
- [luoguP2858] [USACO06FEB]奶牛零食Treats for the Cows(DP)
传送门 f[i][j][k] 表示 左右两段取到 i .... j 时,取 k 次的最优解 可以优化 k 其实等于 n - j + i 则 f[i][j] = max(f[i + 1][j] + a[ ...
- noip模拟赛 算
[问题背景]zhx 帮他妹子做数学题.[问题描述]求: 如 N=3, M=3, 这个值为 1^1+1^2+1^3+2^1+2^2+2^3+3^1+3^2+3^3=56. [输入格式]仅一行, 包含两个 ...
- Swift对象创建过程(PUT Object)——纠删码策略(二)
相应Object使用纠删码(EC)作为存储策略时,BaseObjectController类中PUT和GET需要调用的一些方法会被ECObjectController中相应函数覆盖. 在GET Obj ...
- Oracle删除约束和主键的语句
https://blog.csdn.net/xue_yanan/article/details/78210654?locationNum=8&fps=1
- kendo grid 点击更新没有反映
因为没有在dataSource上写schema schema: { model: { id: "DeptId", fields: { CompanyId: { editable: ...
- 对象和变量的并发访问synchronized解析以及死锁分析排查
一.synchronized java并发编程中存在“非线程安全"问题.“非线程安全"是指发生在多个线程对同一个对象中的实例变量并发访问时,产生的”脏读“现象,使用synchron ...
- 洛谷——P1082 同余方程
P1082 同余方程 题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开. 输出格式: 输 ...