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基础教程(7)--运算符
现在,我们已经学会了如何声明和初始化变量,但你可能想知道如何操作它们.运算符是对一个,两个或三个操作数执行特定操作并返回结果的特殊符号.下表列出了Java中的运算符: 表格中的运算符是按照从上 ...
- 【hdu 2112】 HDU Today ( 最短路 Dijkstra)(map)
http://acm.hdu.edu.cn/showproblem.php?pid=2112 这道题给了一个将字符串与int对应的思路,就是使用map 这道题答案对了,但是没有AC,我也不知道为什么. ...
- 007Spring Security
01.基于Spring AOP 和 Servlet规范中Filter实现 的安全框架 <dependency> <groupId>org.springframework.se ...
- Python爬虫教程-06-爬虫实现百度翻译(requests)
使用python爬虫实现百度翻译(requests) python爬虫 上一篇介绍了怎么使用浏览器的[开发者工具]获取请求的[地址.状态.参数]以及使用python爬虫实现百度翻译功能[urllib] ...
- 7.Java关键字和保留字
一.概念 Java关键字(Key Word): 对Java的编译器有特殊的意义,他们用来表示一种数据类型或者表示程序的结构. 保留字(Reserve Word):即它们在Java现有版本中没有特殊含 ...
- c# webservice中访问http和https的wsdl,生成的配置节点的不同之处
http: https:
- file cycle
# Author:Alex# Date:2017.06.07# Version:3.6.0with open('james.txt') as jaf: data = jaf.readline() ja ...
- java笔记--匿名内部类和静态内部类的理解和使用
匿名内部类 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3889467.html "谢谢-- 1.由于局部内部类并不可见 ...
- Linux 下Wordpress博客搭建
Wordpress # 下载安装文件 cd /usr/local/nginx/html/blog wget https://cn.wordpress.org/wordpress-4.8.1-zh_CN ...
- winform中webBrowser模拟网页操作中遇到的问题
我们通过网页上传一些特殊数据的时候,由于必填项众多,数量量大的时候,会发现工作相当繁琐,前段时间做了一个winform内嵌webBrowser模拟网页上传文档的小工具,发现了许多问题,总结一下: 先说 ...