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 ...
随机推荐
- JavaScript根据国家二字码获取国家全称
function getCountryNameByCode(code) { var countryName = ''; switch(code) { case "US": coun ...
- 获取java根目录,加载根目录下的文件
就两句代码 String filepath = System.getProperty("user.dir")+"/a.xlsx"; File file=new ...
- MIME格式说明,电子邮件格式(转载)
邮件格式说明 Mutiple Internet Mail Extensions Refer to Internet Official Protocol Standards RFC 822 1 概述 网 ...
- 关于iFrame特性总计和iFrame跨域解决办法
1.iframe 定义和用法 iframe 元素会创建包含另外一个文档的内联框架(即行内框架). HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML 1. ...
- 微服务架构之spring cloud gateway
Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射.权限验证.负载均衡 ...
- FineReport如何手动推送APP消息
在报表填报成功后,发送消息至APP会提示数据已更新.再次期间用户需要有查看该模板的权限,如果没有的话,则无法接受到提示信息.那么在FineReport移动端中,如何手动推送APP消息呢? 具体用法 在 ...
- iOS中基于WebView的HTML网页离线访问技术的实现
其实就是MVC模式,视图在在线.离线时可以共用,控制器在在线时是由服务器端实现的,而离线时则是由本地Obj-C代码实现.具体实现方式为采用Mongoose实现. 代码为: mongoose.h mon ...
- 131.006 Unsupervised Learning - Feature Scaling | 非监督学习 - 特征缩放
@(131 - Machine Learning | 机器学习) 1 Feature Scaling transforms features to have range [0,1] according ...
- Linux修改Oracle用戶
Linux下SSH登陆后: su - Oracle; sqlplus /nolog; conn system/密码; 或者 connect/as sysdba; alter user 用户名 iden ...
- 类型安全的EventHandlerList
我们写一个类时,有时候会在同一个类上添加很多事件,事件很多的话,是不容易管理的,.NET提供的EventHandlerList可以辅助多个事件的管理,但不方便的地方是,它不是类型安全的,缺少类型安全, ...