tanchukw

Untitled

Jul 29th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. class Solution {
  2. private:
  3.     int size(int a)
  4.     {
  5.         int c = 0;
  6.         while (a)
  7.         {
  8.             ++c;
  9.             a /= 10;
  10.         }
  11.         return c;
  12.     }
  13. public:
  14.     bool isPalindrome(int x) {
  15.         if (x < 0)
  16.             return 0;
  17.         int sz = size(x), tn = 1;
  18.         for (int i = 0; i < sz-1; ++i)
  19.             tn *= 10;
  20.         for (int i = 0; i < sz / 2; ++i)
  21.         {
  22.             if (x % 10 != (x / tn) % 10)
  23.                 return 0;
  24.             tn /= 100;
  25.             x /= 10;
  26.         }
  27.         return 1;
  28.     }
  29.  
  30.  
  31. };
Advertisement
Add Comment
Please, Sign In to add comment