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. 读写csv文件——考虑各种异常场景,源码

    CSV是以逗号间隔的文本文件,其文件以纯文本形式存储表格数据(数字和文本).在JAVA中可以通过输出文件流的方式将数据写入CSV文件,通过BufferedReader类去读该路径中的文件,使用read ...

  2. mybatis 排坑记录

    1. mapper xml resultMap 中定义 property 时不能出现空格 否则会出现反射错误,找不到 do 对应的 set 方法

  3. RFC1939 POP3协议 中文版 (转载)

      1.简介 对于在网络上的比较小的结点,支持消息传输系统(MTS)是不实际的.例如,一台工作站可能不具有充足的资源允许SMTP服务器和相当的本地邮件传送系统保持序驻留,并持续运行.同样的,将一台个人 ...

  4. Js实现简单的音频播放

    现效果如下: 由于我这边不需要其他按钮,就没写 数据是由后台提供,在这做了个小列子 后台代码 ) { MusicPlayerModel model = new MusicPlayerModel(); ...

  5. CSS,js,html

    图片盗链问题使用以下meta标签解决 <meta name="referrer" content="never"> Chrome 中文界面下默认会将 ...

  6. MAVLink Onboard Integration Tutorial

    MAVLink Onboard Integration Tutorial MAVLink is a header-only library, which means that you don't ha ...

  7. python学习笔记之——正则表达式

    1.re模块 Python通过re模块提供对正则表达式的支持,re 模块使 Python 语言拥有全部的正则表达式功能.使用re的一般步骤是先将正则表达式的字符串形式编译为Pattern实例,然后使用 ...

  8. NuGet 2.0 (.NET软件包管理器) 发布了-现在升级吧

    原文:https://blogs.msdn.microsoft.com/scott_hanselman/2012/07/10/nuget-2-0-net/ [原文发表地址]  NuGet 2.0 (. ...

  9. [转]开源日志库<log4cplus+VS2008使用>整理

    转 开源日志库<log4cplus+VS2008使用>整理 转http://pyhcx.blog.51cto.com/713166/143549 一.简介     log4cplus是C+ ...

  10. 养兔子Fibo函数优化

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...