Power of Four问题及解法
来源:互联网 发布:网络女作家收入排行榜 编辑:程序博客网 时间:2024/06/05 16:14
问题描述:
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
示例:
Given num = 16, return true. Given num = 5, return false.
问题分析:
一个数若是4的幂,那么它一定是2的幂,而且转换成二进制时,1总是在奇数位上,而且对于任意4的幂减去1后都能被三整除。
因而有两种解决方案:
class Solution {public: bool isPowerOfFour(int num) { return (num > 0) && ((num & (num - 1)) == 0) && ((num & 0x55555555) == num); }};class Solution {public: bool isPowerOfFour(int num) { return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0; }};
0 0
- Power of Four问题及解法
- LeetCode342. Power of Four 简单解法
- Power of Three问题及解法
- [LeetCode]Power of Four
- 342. Power of Four
- Power of Four-Leetcode
- LeetCode Power of Four
- LeetCode - Power of Four
- 113.Power of Four
- 342. Power of Four
- LeetCode342. Power of Four
- 342. Power of Four
- Power of Four
- leetcode_342 Power of Four
- LeetCode:Power of Four
- leetcode---Power of Four
- 342. Power of Four
- [LeetCode] Power of Four
- swift3.0 浮点数四舍五入为整数
- ubuntu配置静态IP和DNS
- HDU1561 The more, The Better
- JAVA接口和抽象类
- java的8种排序
- Power of Four问题及解法
- MFC学习总结 (67个技巧)
- Android 开发中我常用的手机App(持续更新)
- ch6.1 data.table 基础
- STL之set的基本用法
- 【JZOJ5043】【NOI2017模拟4.4】保持平衡
- ubuntu下安装flash
- 简单的jquery拖拽排序效果(转载)
- OpenCV检测篇(二)——笑脸检测