LeetCode 342. Power of Four
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?
先说明用循环:
如下
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<)return false;
char position=-;
int i=;
while(i<=)
{
if(num&(<<(i)))
{
if(-!=position&&position!=i)
return false;
position=i;
}
++i;
}
if(position%)return false;
else return true;
}
};
然后我发现题目中提示,你可以不用循环或者递归吗?我就去搜寻了一下这个题目发现http://www.cnblogs.com/grandyang/p/5403783.html
发现第三种方法很巧妙
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 数论
题意:判断一个数是不是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. ...
- 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 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- 342. Power of Four(One-line)
342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...
随机推荐
- 获取LocationProvider
Android的定位信息由LocationProvider对象来提供,该对象代表一个抽象的定位组件.在开始编程之前,需要先获得LocationProvider对象. 一.获取所有可用的Location ...
- pycharm的快捷方式
PyCharm3.0默认快捷键(翻译的)1.编辑(Editing)Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift ...
- apache commons工具包
javqa中,有时候,我们需要重写类的hashCode()和toString()方法,自己去实现,太麻烦. 我们可以用apache的commons工具类来实现. hashCode(): @overri ...
- enmo_day_03
安装 装载点 : /u01 (第一个挂载点 LUN1) /u01 (第二个挂载点LUN2) /disk01 目录 : /u01/app/oracle /u01/app/app 文件 : 控制文件 :c ...
- 64位Win7系统下vs2010调试无法连接oracle
64位win7系统的Program Files (x86)路径中有括号,oracle不认识这样的路径,所以就出现不能连接数据库的问题.所以我们可以将vs2010的内部调试web服务器WebDev.We ...
- iOS 动画 旋转 移动简单代码
#import "ViewController.h" @interface ViewController () { UIImageView *imgView; BOOL flag; ...
- Android 中Thread,Handler,Loop学习
1.先看一下最简单的进度条示例 EG: package com.sxz.android.thread; import java.util.concurrent.atomic.AtomicBoolean ...
- Linux线程-pthread_join
pthread_join用来等待另一个线程的结束,函数原型如下: extern int pthread_join __P ((pthread_t __th, void **__thread_retur ...
- 解决CentOS6.4 Docker "Couldn't connect to Docker daemon ..." 问题
OS: CentOS6.4 #uname -r 2.6.32-504.1.3.el6.x86_64 安装完毕fig,并完成相应配置时执行如下命令出错(fig安装参见:http://www.fig.sh ...
- PAT (Basic Level) Practise:1038. 统计同成绩学生
[题目链接] 本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第1行给出不超过105的正整数N,即学生总人数.随后1行给出N名学生的百分制整数成绩,中间以空格分隔.最 ...