231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂
2024-10-02 07:34:46
231. Power of Two
Given an integer, write a function to determine if it is a power of two.
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > ? (n & (n-)) == : false;
}
};
342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
bool isPowerOfFour(int num) {
static int mask = 0b01010101010101010101010101010101; //edge case
if (num<=) return false; // there are multiple bits are 1
if ((num & num-) != ) return false; // check which one bit is zero, if the place is 1 or 3 or 5 or 7 or 9...,
// then it is the power of 4
if ((num & mask) != ) return true;
return false;
}
最新文章
- 阿里云服务器Linux CentOS安装配置(八)nginx安装、配置、域名绑定
- SDK开发断点失效
- django若干问题
- ELK 部署
- MyEclipse7.0破解下载
- POJ 2253 Frogger (最短路)
- java--从控制台读入一些数据
- 结构-行为-样式-Angularjs-ngSanitize
- Rational Rose 2003 逆向工程转换C++ / VC++ 6.0源代码成UML类图
- SoapUI进行接口测试,怎么应对接口地址总是变化!
- linux命令每日一练:find与rm实现查找并删除目录或文件
- 使用java实现阿里云消息队列简单封装
- Pathon学习笔记1
- vscode——配置git的path
- Nginx访问权限配置
- C#操作IIS站点 Microsoft.Web.Administration.dll
- 项目通过tomcat部署到服务器,请求数据中文乱码问题
- Clover 3 --- Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能。
- linux02
- 在jQuery定义自己函数
热门文章
- HDU 5724 Chess(国际象棋)
- FLASH CC 2015 CANVAS (六)如何像FLASH那样实现场景(多canvas)
- java中判断用户是否为第一次登陆(在页面上进行控制)
- asp.net MVC开发过程中,使用到的方法(内置方法及使用说明)
- Spring security3
- hibernate对象关系实现(一)一对多
- poj2451Uyuw&#39;s Concert(半平面交)
- Linux 系统时间查看 及 时区修改(自动同步时间)
- J2EE 第二阶段项目之编写代码(三)
- Hadoop的HA集群启动和停止流程