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的幂数. ...
随机推荐
- Q-criterion- definition and post-processing
Q-criterion Table of Contents 1. Q-Criterion 1.1. Q-criterion– Hunt, Wray & Moin 1988 1.2. Q cri ...
- 1.Linux标准IO编程
1.1Linux系统调用和用户编程接口 1.1.1系统调用 用户程序向操作系统提出请求的接口.不同的系统提供的系统调用接口各不相同. 继承UNIX系统调用中最基本和最有用的部分. 调用按照功能分:进程 ...
- String类的转换功能
/* * String类的转换功能 * char[] toCharArray():把字符串转换为字符数组 * String toLowerCase():把字符串转换为小写字符串 * String to ...
- 重载与重写的区别----https://blog.csdn.net/zhu_apollo/article/details/1852542
重载 overloading 1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载是一个类中多态性的一种表现. ...
- RabbitMQ-rabbitMq各个特性的使用(三)
准备 1.引入客户端和配置文件依赖类 <dependency> <groupId>com.rabbitmq</groupId> <artifactId> ...
- mongodb replica set 添加/删除节点方法--http://www.ii123.com/jc/bc/bczh/258948.html
replica set多服务器主从,添加,删除节点,肯定会经常遇到的.下面详细说明一下,添加,删除节点的2种方法. 一,利用rs.reconfig,来添加,删除节点 1,添加节点 代码如下 re ...
- 22、Java并发性和多线程-Java中的读/写锁
以下内容转自http://ifeve.com/read-write-locks/: 相比Java中的锁(Locks in Java)里Lock实现,读写锁更复杂一些.假设你的程序中涉及到对一些共享资源 ...
- logout退出功能是怎么实现的?login登陆功能室怎么实现的
logout退出功能是怎么实现的?login登陆功能室怎么实现的 login就是登陆成功的时候,在session里面创建好用户对应的数据. logout就是登出的时候,在session里面销毁用户对应 ...
- leetcode笔记:Wiggle Sort
一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nu ...
- JavaScript Patterns 2.7 Avoiding Implied Typecasting
Dealing with == and === false == 0 or "" == 0 return true. always use the === and !== oper ...