Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

C++:
01 class Solution {
02 public:
03     bool isPalindrome(int x) {
04         // Start typing your C/C++ solution below
05         // DO NOT write int main() function
06         if(x<0) return false;
07         int k=1;
08         while(x/k>=10)
09         {
10             k=k*10;
11         }
12         while(x>=10)
13         {
14             int p=x/k;
15             int q=x%10;
16             if(p!=q)    return false;
17             x=(xp*kq)/10;
18             k=k/100;
19         }
20         return true;
21     }
22 };
Advertisements
This entry was posted in Number trick. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s