![]() |
VOOZH | about |
Given an integer x, determine whether its binary representation is a palindrome. Return true if it is a palindrome; otherwise, return false.
Input:
x = 9
Output:true
Explanation: The binary representation of9is1001, which is a palindrome.
Input:x = 10
Output:false
Explanation: The binary representation of10is1010, which is not a palindrome.
Compare bits from the leftmost and rightmost positions. If all corresponding bits match, the binary representation is a palindrome.
true false
Time Complexity: O(log x), as the loop runs once for each bit in x.
Space Complexity: O(1).
Convert the integer to its binary string form and check if it is equal to its reverse using the reverse() function.
true false
Time Complexity: O(log x), as operations are performed on all bits of x.
Space Complexity: O(log x), due to storing the binary string and its reverse.
Reverse the binary representation using bitwise operations and compare it with the original number.
true false
Time Complexity: O(log x)
Space Complexity: O(1)