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的整数次幂的更多相关文章

  1. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  2. [LeetCode] 326. Power of Three + 342. Power of Four

    这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...

  3. 342. Power of Four(One-line)

    342. Power of Four     Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...

  4. 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. ...

  5. 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 ...

  6. 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 ...

  7. 【一天一道LeetCode】#342. Power of Four

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 样式表中的 element.style样式如何修改

    我们在写前面 web样式的时候,会发现有些时候,我们怎么修改 style里面的值,页面上的样式都不会修改,当你用工具查看时,会发现里面会有 element.style的值,这个值还找不到是在哪里出现的 ...

  2. bootstrp水平表单格式

    1:form里添加类 "form-horizontal" 2:把标签和表单控件(input 等)放在一个带有类名 "form-group"的div里 3:标签可 ...

  3. BP神经网络求解异或问题(Python实现)

    反向传播算法(Back Propagation)分二步进行,即正向传播和反向传播.这两个过程简述如下: 1.正向传播 输入的样本从输入层经过隐单元一层一层进行处理,传向输出层:在逐层处理的过程中.在输 ...

  4. Creating Excel File in Oracle Forms

    Below is the example to create an excel file in Oracle Forms.Pass the Sql query string to the below ...

  5. EasyUI--messager

    1. alert 方法 <script type="text/javascript"> $( function() { $.messager.alert("调 ...

  6. js判断ie11浏览器

    var isIE11 = (/Trident\/7\./).test(navigator.userAgent);

  7. tilemap坐标转换

    像素点跟tile的索引之间的转换//从cocos2d-x坐标转换为Tilemap坐标CCPoint GameMap::tileCoordForPosition(CCPoint position){ i ...

  8. 泰信---->20131025~20131101(周计划安排)

    学习java相关知识计划安排: [前期复习阶段] (一)熟悉java相关语言基础. (二)搭建一个纯web项目工程,实现增加,删除,修改,查询以及分页...功能. (三)熟悉jboss的使用和相关配置 ...

  9. 跨代的对决 英特尔i7-6700HQ对比i7-4720HQ性能测试

    http://itianti.sinaapp.com/index.php/cpu 跨代的对决 英特尔i7-6700HQ对比i7-4720HQ性能测试 2015-10-13 19:46:31 来源:电脑 ...

  10. Python学习笔记--XML的应用

    XML的定义 XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没有被 ...