Palindrome Number II

Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation of the number is being considered for palindrome checking, no leading 0’s are being considered.

Solution: Reverse Binary

  1. Reverse target number
  2. Check palindrome
    bool isPalindrome(int n) {
        // Write your code here
        int reverseN = 0, temp = n;
        while (temp !=0) {
            reverseN <<= 1;
            if (temp & 1) {
                reverseN ^= 1;
            }
            temp >>= 1;
        }

        return n == reverseN;
    }

results matching ""

    No results matching ""