Skip to content

Commit cf492d5

Browse files
committed
feat: solve No.342
1 parent 9697272 commit cf492d5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

301-400/342. Power of Four.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 342. Power of Four
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Math, Bit Manipulation, Recursion.
5+
- Similar Questions: Power of Two, Power of Three.
6+
7+
## Problem
8+
9+
Given an integer `n`, return **`true` if it is a power of four. Otherwise, return `false`**.
10+
11+
An integer `n` is a power of four, if there exists an integer `x` such that `n == 4x`.
12+
13+
 
14+
Example 1:
15+
```
16+
Input: n = 16
17+
Output: true
18+
```Example 2:
19+
```
20+
Input: n = 5
21+
Output: false
22+
```Example 3:
23+
```
24+
Input: n = 1
25+
Output: true
26+
```
27+
 
28+
**Constraints:**
29+
30+
31+
32+
- `-231 <= n <= 231 - 1`
33+
34+
35+
 
36+
**Follow up:** Could you solve it without loops/recursion?
37+
38+
## Solution
39+
40+
```javascript
41+
/**
42+
* @param {number} n
43+
* @return {boolean}
44+
*/
45+
var isPowerOfFour = function(n) {
46+
var num = 0b1010101010101010101010101010101;
47+
return n > 0 && (n & (n - 1)) === 0 && (n & num) === n;
48+
};
49+
```
50+
51+
**Explain:**
52+
53+
nope.
54+
55+
**Complexity:**
56+
57+
* Time complexity : O(1).
58+
* Space complexity : O(1).

0 commit comments

Comments
 (0)