Skip to content

Commit 1bd47da

Browse files
committed
Add Power of Four C#
1 parent ef50f1b commit 1bd47da

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
//Solution 1
3+
public bool IsPowerOfFour(int num) {
4+
if(num > 1)
5+
{
6+
while(num % 4 == 0){
7+
num /= 4;
8+
}
9+
}
10+
return num == 1;
11+
}
12+
13+
//Solution 2
14+
public bool IsPowerOfFour(int num) {
15+
//0x55555555 (hex) means 0101 0101 0101 0101 0101 0101 0101 0101 (binary)
16+
return (num > 0)
17+
&& (num & (num-1)) == 0
18+
&& ((num & 0x55555555) != 0);
19+
}
20+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Solutions in various programming languages are provided. Enjoy it.
1010
1. [Detect Capital](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/01-Detect-Capital)
1111
2. [Design HashSet](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/02-Design-HashSet)
1212
3. [Valid Palindrome](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/03-Valid-Palindrome)
13+
4. [Power of Four](https://github.com/AlgoStudyGroup/Leetcode/tree/master/August-LeetCoding-Challenge/04-Power-of-Four)
1314

1415
## July LeetCoding Challenge
1516
Click [here](https://leetcode.com/explore/featured/card/july-leetcoding-challenge/) for problem descriptions.

0 commit comments

Comments
 (0)