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. 购买vps创建账号后无法登录ftp

    购买了vps,并在后台已经开通了ftp账号,但是前台无法登录.错误提示530.解决方法是: 请检查您ftp账号密码是否输入正确,若确认正确,请您ssh登陆服务器,然后执行sh /www/wdlinux ...

  2. 请求webservice接口的某方法数据

    NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.com/xxx/xxxxWS?wsdl"]; NSString *soapMs ...

  3. git bash下的选择、复制、粘贴

      1. 打开git bash     2.点击左上角,选择属性,打钩     3.回到界面,选择一行文字,然后点击 “右键”,这样就复制到剪切板了.再点 “右键”,可以粘贴到命令窗口中了.    

  4. How can I retrieve the remote git address of a repo?

    When you want to show an URL of remote branches, try: git remote -v

  5. Codeforces Round #375 (Div. 2) C. Polycarp at the Radio 贪心

    C. Polycarp at the Radio time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  7. 容易混淆的url src href

    新手刚学习的时候会分不清 url  src  href这些,不知道什么情况下应该用哪个.现在让我来理一理. url 统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网 ...

  8. JMS【三】--ActiveMQ简单的HelloWorld实例

    第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...

  9. 数据库mysql中distinct关键词

    在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 例如,在学生信息表中,你想查询该学校有多少个系.例如, ...

  10. PL/SQL 听课笔记

    PL/SQL: 知识回顾: SQL:  结构化查询语言: T-SQL:  microsoft sql语言: PL/SQL: Oracle sql语言: 变量命名规则: 1.首字母必须是字母,可以包含字 ...