Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Certainly! The problem "Power of Four" on LeetCode asks you to determine if a given integer is a power of four. A number is a power of four if it can be expressed as
- 4
- n
- 4
- n
- where
- n
- n is a non-negative integer.
- Here's a simple C++ solution to solve this problem:
- class Solution {
- public:
- bool isPowerOfFour(int n) {
- // Check if n is positive and if the bitwise representation of n has only one bit set
- // and that bit is in an even position (which corresponds to powers of four)
- return n > 0 && (n & (n - 1)) == 0 && (n & 0xAAAAAAAA) == 0;
- }
- };
- Explanation:
- Check if n is positive: A power of four must be greater than zero.
- Check if n is a power of two: The expression (n & (n - 1)) == 0 checks if n is a power of two. This works because powers of two have exactly one bit set in their binary representation.
- Check if the bit is at an even position: The expression (n & 0xAAAAAAAA) == 0 ensures that the only set bit (if it exists) is in an even position. The hexadecimal 0xAAAAAAAA corresponds to the binary representation 10101010101010101010101010101010, which has bits set at the positions of powers of four.
- Example Usage:
- You can test this function using the following main function:
- #include <iostream>
- int main() {
- Solution solution;
- int n;
- // Test with some examples
- n = 16; // 4^2
- std::cout << "Is " << n << " a power of four? " << (solution.isPowerOfFour(n) ? "Yes" : "No") << std::endl;
- n = 5; // Not a power of four
- std::cout << "Is " << n << " a power of four? " << (solution.isPowerOfFour(n) ? "Yes" : "No") << std::endl;
- n = 64; // 4^3
- std::cout << "Is " << n << " a power of four? " << (solution.isPowerOfFour(n) ? "Yes" : "No") << std::endl;
- return 0;
- }
- Output:
- Is 16 a power of four? Yes
- Is 5 a power of four? No
- Is 64 a power of four? No
- This code correctly determines whether the given integer is a power of four.
- write code C# leetcode 342. Power of Four
- Certainly! Here’s a C# solution for the LeetCode problem 342, "Power of Four". The approach is similar to the C++ solution, where we check if the number is a power of four using bit manipulation.
- C# Code:
- public class Solution {
- public bool IsPowerOfFour(int n) {
- // Check if n is positive, a power of two, and that the single bit is in an even position
- return n > 0 && (n & (n - 1)) == 0 && (n & 0xAAAAAAAA) == 0;
- }
- }
- Explanation:
- Positive Check: We first check if n is greater than 0, as powers of four are positive integers.
- Power of Two Check: The condition (n & (n - 1)) == 0 checks if n is a power of two. This works because powers of two have exactly one bit set.
- Even Position Check: The condition (n & 0xAAAAAAAA) == 0 ensures that the set bit is in an even position. The hexadecimal 0xAAAAAAAA corresponds to the binary 10101010101010101010101010101010, which has bits set at the positions of powers of four.
- Example Usage:
- You can test this function in a console application:
- using System;
- public class Program {
- public static void Main() {
- Solution solution = new Solution();
- // Test with some examples
- int[] testCases = { 16, 5, 64, 1, 0, -4 };
- foreach (int n in testCases) {
- Console.WriteLine($"Is {n} a power of four? {solution.IsPowerOfFour(n)}");
- }
- }
- }
- Output:
- Is 16 a power of four? True
- Is 5 a power of four? False
- Is 64 a power of four? False
- Is 1 a power of four? True
- Is 0 a power of four? False
- Is -4 a power of four? False
- This code provides a method to determine if a given integer is a power of four and includes a test harness to demonstrate its functionality.
- Certainly! Here’s a Python solution for the LeetCode problem 342, "Power of Four". The approach is similar to the previous solutions, where we check if the number is a power of four using bit manipulation.
- Python Code:
- class Solution:
- def isPowerOfFour(self, n: int) -> bool:
- # Check if n is positive, a power of two, and that the single bit is in an even position
- return n > 0 and (n & (n - 1)) == 0 and (n & 0xAAAAAAAA) == 0
- Explanation:
- Positive Check: We first check if n is greater than 0, as powers of four are positive integers.
- Power of Two Check: The expression (n & (n - 1)) == 0 checks if n is a power of two. This works because powers of two have exactly one bit set in their binary representation.
- Even Position Check: The expression (n & 0xAAAAAAAA) == 0 ensures that the set bit is in an even position. The hexadecimal 0xAAAAAAAA corresponds to the binary representation 10101010101010101010101010101010, which has bits set at the positions of powers of four.
- Example Usage:
- You can test this function with the following example:
- if __name__ == "__main__":
- solution = Solution()
- # Test with some examples
- test_cases = [16, 5, 64, 1, 0, -4]
- for n in test_cases:
- print(f"Is {n} a power of four? {solution.isPowerOfFour(n)}")
- Output:
- Is 16 a power of four? True
- Is 5 a power of four? False
- Is 64 a power of four? False
- Is 1 a power of four? True
- Is 0 a power of four? False
- Is -4 a power of four? False
- This code defines a method to determine if a given integer is a power of four and includes a test harness to demonstrate its functionality.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement