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. 小程序异步处理demo计时器setInterval()

    实现一个计时器/秒 其实就是要求对某字段每秒执行一次更新 这里用到了官方给的定时器 官方API 每秒刷新一次,所以用setInterval()方法 下面给出关键代码: 由于无关代码过多,这里尽可能贴出 ...

  2. spring-cloud-sleuth简单使用

    快速开始 一.导入依赖 <!--链路追踪 start--> <dependency> <groupId>org.springframework.cloud</ ...

  3. groovy函数、字符串、循环

    三重单引号字符串 '''a triple single quoted string''' 三重单引号字符串是普通的java.lang.String 三重单引号字符串是多行的.您可以跨越行边界跨越字符串 ...

  4. JQuery实现获取多个input输入框的值,并存放在一个数组中

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

  5. C#语言数据类型

    整数类型 sbyte   -128~127之间 byte   0~255 short(Int16) -32768~32768 ushort(UInt16)  0~65535 Int (Int32)   ...

  6. js中常用的算法排序

    在工作中都会经常用到的一些基础算法,可以很快解决问题.这些都是在工作中总结的,希望可以帮助到大家. 一.数组乱序 arr.sort(function randomsort(a, b) { return ...

  7. 使用postMessage通信,未触发message事件

    前提: 父子页面跨域通信,使用postMessage技术 a页面为父页面,b页面为子页面 a中包含多个iframe,部分域名是相同的,目录层级不一致,地址使用变量根据触发的条件不同拼接地址 部分代码( ...

  8. 前端面试题1(html)

    HTML     * Doctype作用?严格模式与混杂模式如何区分?它们有何意义? 1.<!DOCTYPE> 声明位于文档中的最前面的位置,处于 <html> 标签之前.此标 ...

  9. Node.js学习(篇章一)

    <node.js的特点> 采用了异步式I/O与事件驱动的架构设计,架构为单线程模型. <supervisor包的作用> node.js开发项目,当修改项目时,需要终止进程重启N ...

  10. No value specified for 'Date'错误

    今天使用 BeanUtils.copyProperties(m,n);  遇到  No value specified for 'Date'  这个错误,以前用的时候都不需要加 try 今天使用发现需 ...