342. Power of Four
2016-05-22 11:37
197 查看
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?
Analysis:
用了一种大概比较‘笨’的方法,把所有可能值全部列举出来,但也不失为一种方法。
关于其他方法详见:326. Power of Three 和 231. Power of Two
Source Code(C++):
#include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution { public: bool isPowerOfFour(int num) { vector<int> v; v.push_back(0x00000001);v.push_back(0x00000004);v.push_back(0x00000010);v.push_back(0x00000040);v.push_back(0x00000100);v.push_back(0x00000400);v.push_back(0x00001000);v.push_back(0x00004000); v.push_back(0x00010000);v.push_back(0x00040000);v.push_back(0x00100000);v.push_back(0x00400000);v.push_back(0x01000000);v.push_back(0x04000000);v.push_back(0x10000000);v.push_back(0x40000000); if (find(v.begin(), v.end(), num) == v.end()){ return false; } else { return true; } } }; int main() { Solution sol; cout << sol.isPowerOfFour(16); return 0; }
相关文章推荐
- [BZOJ1006][HNOI2008]神奇的国度(弦图最小染色)
- datetimepicker 插件用法及参数说明
- cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析
- mongoVUE 连接MongoDB3.2.2不能正常显示collection的问题
- Objective-C ---代理和协议 (梳理整理)
- 谷歌约束控件(ConstraintLayout)扁平化布局入门
- Linux时间日期设置相关命令
- 序列模型-RNN
- 还在用ListView?!OutMan!RecyclerView!
- 二叉树相关笔试题(三)
- 扩展欧几里德求逆元,快速幂 hdoj 1211
- 第101讲: 使用Spark Streaming企业实际数据处理流水线完整声明周期
- java 技术体系
- Android.mk用法整理
- IOS FFMPEG的使用
- Azure show and Azure-Samples
- 前端性能优化
- dpi对应图标分辨率
- JAVA之使用JDBC连接数据库-使用jtds 连接Sql Server 2005数据库
- HDU 5029 Relief grain 树链剖分 好题