sweet1cris

Untitled

Feb 10th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1.  
  2. public class Solution {
  3.     /**
  4.      * @param n: non-negative integer n.
  5.      * @return: return whether a binary representation of a non-negative integer n is a palindrome.
  6.      */
  7.     public boolean isPalindrome(int n) {
  8.         // write code here.
  9.         int[] bin = new int[32];
  10.         int len = 0;
  11.         do {
  12.             bin[len++] = n & 1;
  13.             n >>= 1;
  14.         } while (n > 0);
  15.         for (int i = 0; i < len / 2; i++) {
  16.             if (bin[i] != bin[len - i - 1]) {
  17.                 return false;
  18.             }
  19.         }
  20.         return true;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment