p342

LeetCode p342 Power of Four 题解

1.题目:

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?

题意:

判断一个数是否为4的n次方

2.解题思路:

见代码,枚举。

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
public class Solution {
public boolean isPowerOfFour(int num) {
List<Integer> list = new ArrayList<Integer>();
int sum = 1;
for (int i = 1; i < 17; i++) {
list.add(sum);
sum = sum * 4;

}
if (list.contains(num))
return true;
return false;
}
}

4.一些总结: