1.2的幂

正确写法:

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= )
return false;
return (n & (n-)) == ;
}
};

错误写法1:

&符号的短路原则,如果&前面为false了就不会计算后面的了

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= )
return false;
retur
class Solution {
public:
bool isPowerOfFour(int num) {
if(num <= )
return false;
if((num & (num - )) == ){
if(num & 0x55555555)
return true;
else
return false;
}
else
return false;
}
};

n ((n-) & n) == ;
}
};

错误写法2

==符号的优先级比&高

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= )
return false;
return n & (n-) == ;
}
};

2.4的幂

class Solution {
public:
bool isPowerOfFour(int num) {
if(num <= )
return false;
if((num & (num - )) == ){
if(num & 0x55555555)
return true;
else
return false;
}
else
return false;
}
};

3.3的幂

https://blog.csdn.net/u014218090/article/details/80152446

leetcode231 2的幂 leetcode342 4的幂 leetcode326 3的幂的更多相关文章

  1. leetcode342合理运用位操作判断4的幂

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  2. [Swift]LeetCode326. 3的幂 | Power of Three

    Given an integer, write a function to determine if it is a power of three. Example 1: Input: 27 Outp ...

  3. 幂的运算:X的n次幂

    计算X的n次幂,有多种算法 例子:计算2的62次方. method 1 :time = 1527 纳秒. 常规思路,进行61次的乘法! private static long mi(long X, l ...

  4. 二分求幂,快速求解a的b次幂

    一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) { int a, b; ; cin >> a >> b; ; i < ...

  5. 51 Nod 1013 3的幂的和 矩阵链乘法||逆元+快速幂

    这道题我写了两种写法 一种利用逆元 a/b%mod=a*c%mod; (c是b的逆元)易得2的逆元就是5~~~04: 一种是矩阵快速幂 利用递推式得出结论 #include<cstdio> ...

  6. 快速幂取模模板 && 51nod 1013 3的幂的和

    #include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...

  7. 大数低速幂运算模板(c++)+python大数幂

    简介 自己从大数加法改过来的模板,低速计算n的t次幂,n,t小于等于100速度能够保证 模板 #include <bits/stdc++.h> using namespace std; s ...

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

  9. C语言 · 2的次幂表示

    问题描述 任何一个正整数都可以用2进制表示,例如:137的2进制表示为10001001. 将这种2进制表示写成2的次幂的和的形式,令次幂高的排在前面,可得到如下表达式:137=2^7+2^3+2^0 ...

随机推荐

  1. 四、cent OS安装配置mysql

    下载mysql的repo源wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 安装mysql-community-r ...

  2. Google Voice号码使用说明及用途

    Google Voice号码使用说明及用途 号码 已不能网页注册获取,直接向TB购买,搜Google Voice就行了.很便宜的. 一. Google Voice介绍 Google Voice首先是一 ...

  3. java EE 新手入门了解

    郑重申明:本文转载至https://blog.csdn.net/Neuf_Soleil/article/details/80962686,在此深表感谢! 为什么选择java? 想必有很多初学者会像我一 ...

  4. hdu 1075 What Are You Talking About 字典树模板

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  5. K:括号分隔符匹配问题

    相关介绍:  括号分隔符匹配问题是指,判断所输入的字符串表达式中的括号是否匹配的问题,例如1+(12+2)*(1+2)便是一个括号分隔符匹配的表达式,而(12+1)*4+(12/2]就是一个括号分隔符 ...

  6. 精选10款HTML5手机模板

    1.Stroller | Mobile & Tablet Responsive Template 演示地址   购买地址 2.Ocean Mobile Template 演示地址   购买地址 ...

  7. c# 圆上坐标点

    var x=-33204.0924438;  //圆心x var y=-9512.41208658; //圆心y var r=1000;//半径 var angle=30;//角度 var tmpX ...

  8. Angular面试题一

    一.ng-show/ng-hide 与 ng-if的区别? 第一点区别是, ng-if 在后面表达式为 true 的时候才创建这个 dom 节点, ng-show 是初始时就创建了,用 display ...

  9. 《Linux命令行与Shell脚本编程大全第2版》读书笔记

    公司说不准用云笔记了,吓得我赶紧把笔记贴到博客上先..... 近3年前的了,只有一半的章节,后面的没空记录了.... 第1章 可以cat /proc/meminfo文件来观察Linux系统上虚拟内存的 ...

  10. 剑指Offer-编程详解-二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...