Power of 4
Title description: Given an integer, write a function to determine whether it is a power of 4. If yes, return true; otherwise, return false.
If the integer n is a power of 4, it must satisfy: there is an integer x such that n == $4^{x}$
Please refer to the official website of LeetCode for examples.
Source: LeetCode
Link: https://leetcode-cn.com/problems/power-of-four/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Round Robin
First, if n is equal to 0, then return false directly.
If n is not equal to 0, the loop processing is performed, and the processing procedure is as follows:
- If n is not divisible by 4, return false directly;
- Otherwise, reset n to
n/4
, and then proceed to the next round of processing;- The condition for the end of the loop is that n is less than 4.
Finally, it is judged that if n is equal to 1, it returns true; otherwise, it returns false.
: and -326-3 161472652aa51f is exactly the same.
public class LeetCode_342 {
public static boolean isPowerOfFour(int n) {
// 4的任何次幂都不可能为0,所以直接直接返回false
if (n == 0) {
return false;
}
while (n >= 4) {
if (n % 4 != 0) {
return false;
}
n = n / 4;
}
if (n == 1) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
System.out.println(isPowerOfFour(16));
}
}
【Daily Message】 Life is a kind of rhythm. There must be light and shadow, left and right, sunny and rainy, and the taste is contained in this change but not fierce twists and turns.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。