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 Solution {
public:
bool isPowerOfTwo(int n) {
return n > ? (n & (n-)) == : false;
}
};
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?
bool isPowerOfFour(int num) {
static int mask = 0b01010101010101010101010101010101;
//edge case
if (num<=) return false;
// there are multiple bits are 1
if ((num & num-) != ) return false;
// check which one bit is zero, if the place is 1 or 3 or 5 or 7 or 9...,
// then it is the power of 4
if ((num & mask) != ) return true;
return false;
}
231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂的更多相关文章
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- [LeetCode] 326. Power of Three + 342. Power of Four
这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...
- 342. Power of Four(One-line)
342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...
- LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. ...
- Q&A in Power BI service and Power BI Desktop
What is Q&A? Sometimes the fastest way to get an answer from your data is to ask a question usin ...
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- 【一天一道LeetCode】#342. Power of Four
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 ...
随机推荐
- COM/ATL 资料收集
COM/ATL COM基础知识 COM技术分类
- 当插入数据失败时,防止mysql自增长字段的自增长的方法
问题描述: 当mysql设置了自增长字段时(注意:一个表中只能设置一个自增长字段,可以不是主键,但必须是键 ),如果插入数据失败,那么自增长字段仍然会占用这个自增长值,再次成功插入数据时就会造成断层. ...
- U盘文件后缀变成.exe怎么办?
现在U盘病毒90%都是kido病毒,中了kido病毒的U盘文件名会变成可执行文件,后缀带有exe,虽然杀毒软件可以杀掉,但都是没办法处理它的母体,由于此类文件并没有丢失,而是被隐藏了,因此我们可以靠手 ...
- SQL 批量删除数据表
) while(exists(select * from sysobjects where name like '表名前缀%')) begin select @name=name from sysob ...
- hdu 1800 (map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/ ...
- iOS问题处理:如何在Mac下显示Finder中的所有文件
摘自:http://www.cnblogs.com/elfsundae/archive/2010/11/30/1892544.html 在Unix下工作,你可能需要处理一些“特殊“文件或文件夹,例如/ ...
- ZOJ-3725 Painting Storages 动态规划
题意:给定一个数N,表示有N个位置,要么放置0,要么放置1,问至少存在一个连续的M个1的放置方式有多少? 分析:正面求解可能还要考虑到重复计算带来的影响,该题适应反面求解.设dp[i][j]表示到前 ...
- iOS - UIAlertView
前言 NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a p ...
- java的重修之路
一.内存管理 java里的声明分引用与基本数据类型. 数组: java里new一个对象数组为 person[] A; A = new person[4]; person[0] = new pers ...
- hiho_1057_performance_log
题目大意 给出一个函数调用日志,判断日志是否合法,且求出合法日志中函数调用的时间长度. 题目链接:performance log 题目分析 首先需要清除非法日志的几种情形: (1)日志的时间戳不是按照 ...