每天一道LeetCode--342. Power of Four
2016-11-23 15:27
204 查看
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?
Example:
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
public class Solution { public boolean isPowerOfFour(int num) { int n=num; while(n>0&&n%4==0){ n/=4; } return n==1; //return (num > 0) && ((num & (num - 1)) == 0) && ((num & 0x55555555) == num); } }
相关文章推荐
- 【一天一道LeetCode】#342. Power of Four
- 【一天一道LeetCode】#342. Power of Four
- 每天一道LeetCode-----将字符串切分成若干单词,使得每个单词都在给定的字典中,求出所有的切分结果
- 每天一道LeetCode-----找出给定序列的所有子序列
- 每天一道LeetCode-----实现一个栈,提供一个接口用于返回栈中最小值
- 每天一道LeetCode----位运算实现加减乘除四则运算
- 每天一道LeetCode-----重新实现next_permutation
- 每天一道LeetCode-----将有序序列转成高度平衡二叉搜索树
- 每天一道题:LeetCode
- 每天一道LeetCode-----给定大小为n+1的数组,元素大小在[1 : n]之间,只有一个元素会重复出现多次,找到重复的那个
- 每天一道LeetCode-----以字符串的形式输出二叉树所有从根节点到叶子节点的路径
- [LeetCode] 342. Power of Four(位操作)
- 每天一道LeetCode--119.Pascal's Triangle II(杨辉三角)
- 每天一道LeetCode-----重新实现开方运算sqrt(x),只返回整数部分即可
- 每天一道LeetCode-----分糖果问题
- 每天一道LeetCode-----复制一个带有随机节点的链表
- 每天一道LeetCode-----在字符方格中查找某个单词
- LeetCode 342. Power of Four
- LeetCode -- 342. Power of Four
- 每天一道LeetCode-----寻找给定字符串中重复出现的子串