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 ...
随机推荐
- java中判断从数据库中取出的字段是否为空
方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.1:if(s == null || s.equals(""));方法二: 比较字符串长度, 效率高, 是我知道的最好一 ...
- [HDU5907]Find Q(水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5907 记下每块大小,然后n*(n+1)/2 #include <bits/stdc++.h> ...
- ARM工作模式
ARM工作模式 学习ARM的最好的资料是ARM公司发布的资料:ARM Architecture Reference Manual.pdf 找到章节:Programmers’ Model 由文档可知:A ...
- hdu 1086(计算几何入门题——计算线段交点个数)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2 ...
- 3----lua的数据转换及运算符
lua的基本数据类型转换 转换成字符串 tostring( ... ) 可以将布尔类型和数字类型的值转换为字符串类型的值 local num=1; print(type(num)) newNum = ...
- perl的map函数
perl的map函数的使用: 语法 map EXPR, LIST map BLOCK LIST 定义和使用 对list中的每个元素执行EXPR或BLOCK,返回新的list.对每一此迭代,$_中保存了 ...
- C#正则表达式编程(二):Regex类用法
上一篇讲述了在C#中有关正则表达式的类之间的关系,以及它们的方法,这一篇主要是将Regex这个类的用法的,关于Match及MatchCollection类会在下一篇讲到.对于正则表达式的应用,基本上可 ...
- jquery使用技巧
1. 禁用右键点击(Disable right-click) <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- mac下pmset的使用方法
在终端输入:pmset -g custom 这个命令可以显示系统所有的电源设置 lidwake: 当屏幕掀开的时候唤醒Mac,1是开启 0是关闭autopoweroff: 如果Mac处于睡眠状 ...
- [LED]如何配置LCD背光和LED,调试方法
[DESCRIPTION] 如何配置LCD背光和LED,调试方法 [SOLUTION]LCD背光和LED配置文件alps/custom/<proj name>lk/cust_leds.ca ...