leetcode231 2的幂 leetcode342 4的幂 leetcode326 3的幂
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的幂的更多相关文章
- leetcode342合理运用位操作判断4的幂
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...
- [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 ...
- 幂的运算:X的n次幂
计算X的n次幂,有多种算法 例子:计算2的62次方. method 1 :time = 1527 纳秒. 常规思路,进行61次的乘法! private static long mi(long X, l ...
- 二分求幂,快速求解a的b次幂
一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) { int a, b; ; cin >> a >> b; ; i < ...
- 51 Nod 1013 3的幂的和 矩阵链乘法||逆元+快速幂
这道题我写了两种写法 一种利用逆元 a/b%mod=a*c%mod; (c是b的逆元)易得2的逆元就是5~~~04: 一种是矩阵快速幂 利用递推式得出结论 #include<cstdio> ...
- 快速幂取模模板 && 51nod 1013 3的幂的和
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...
- 大数低速幂运算模板(c++)+python大数幂
简介 自己从大数加法改过来的模板,低速计算n的t次幂,n,t小于等于100速度能够保证 模板 #include <bits/stdc++.h> using namespace std; s ...
- 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. ...
- C语言 · 2的次幂表示
问题描述 任何一个正整数都可以用2进制表示,例如:137的2进制表示为10001001. 将这种2进制表示写成2的次幂的和的形式,令次幂高的排在前面,可得到如下表达式:137=2^7+2^3+2^0 ...
随机推荐
- 撩课-Java每天10道面试题第5天
41.Iterator.ListIterator 和 Enumeration的区别? 迭代器是一种设计模式, 它是一个对象, 它可以遍历并选择序列中的对象, 而开发人员不需要了解 该序列的底层结构. ...
- HDU 2544(简单最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2544 /* 使用pair代替结构 */ #include <iostream> #include & ...
- mybatis oracle 顺序模糊匹配
前言:有时需要顺序模糊匹配字段. 用一半的 % 就好: t.item like #{item}||'%'
- CentOS6.5下连网以及输入法下载
宽带拨号连网: 1.系统--首选项--网络连接(或点击桌面右上角连网图标--VPN连接--VPN配置) 2.添加--选择DSL--勾自动连接(也可不勾)--DSL下填写用户名.密码--应用 ...
- 20个网页设计师应该学习的CSS3经典教程实例
CSS3技术离我们越近,我们也应该学习一些简单的CSS3技术了,而学习最基本的方法就是模仿,以及观看大师作品的案例.收集了20个基础教程,均是涉及到css3应用范围,值得你和我一起共同学习. Smoo ...
- 01.css选择器-->类选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Sql 列转行字符串
select OrderID,ProdDetailID from A 表A : OrderID,ProdDetailID 1 6 1 7 1 ...
- <Android 应用 之路> MPAndroidChart~PieChart
简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...
- c# 序列化接口(转载贴)
http://www.cnblogs.com/TianFang/p/3724449.html
- UIResponder NSSet UITouch UIEvent
UIResponder: UIView的超类,用来响应handle(触屏.motion.响应者等)事件. NSSet:一系列的类集合(类似数组). UITouch:一个点击类.负责:点击的view,w ...