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;
}

最新文章

  1. 阿里云服务器Linux CentOS安装配置(八)nginx安装、配置、域名绑定
  2. SDK开发断点失效
  3. django若干问题
  4. ELK 部署
  5. MyEclipse7.0破解下载
  6. POJ 2253 Frogger (最短路)
  7. java--从控制台读入一些数据
  8. 结构-行为-样式-Angularjs-ngSanitize
  9. Rational Rose 2003 逆向工程转换C++ / VC++ 6.0源代码成UML类图
  10. SoapUI进行接口测试,怎么应对接口地址总是变化!
  11. linux命令每日一练:find与rm实现查找并删除目录或文件
  12. 使用java实现阿里云消息队列简单封装
  13. Pathon学习笔记1
  14. vscode——配置git的path
  15. Nginx访问权限配置
  16. C#操作IIS站点 Microsoft.Web.Administration.dll
  17. 项目通过tomcat部署到服务器,请求数据中文乱码问题
  18. Clover 3 --- Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能。
  19. linux02
  20. 在jQuery定义自己函数

热门文章

  1. HDU 5724 Chess(国际象棋)
  2. FLASH CC 2015 CANVAS (六)如何像FLASH那样实现场景(多canvas)
  3. java中判断用户是否为第一次登陆(在页面上进行控制)
  4. asp.net MVC开发过程中,使用到的方法(内置方法及使用说明)
  5. Spring security3
  6. hibernate对象关系实现(一)一对多
  7. poj2451Uyuw&#39;s Concert(半平面交)
  8. Linux 系统时间查看 及 时区修改(自动同步时间)
  9. J2EE 第二阶段项目之编写代码(三)
  10. Hadoop的HA集群启动和停止流程