码迷,mamicode.com
首页 > 其他好文 > 详细

Palindrome Number

时间:2015-08-14 18:35:55      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

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

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if(x<0) return false;
 5         if(x==0) return true;
 6         vector<int> v;
 7         while(x!=0)
 8         {
 9             v.push_back(x%10);
10             x=x/10;
11         }
12         int n=v.size();
13         int left=0;
14         int right=n-1;
15         while(left<right)
16         {
17             if(v[left]!=v[right])
18                 return false;
19             left++;
20             right--;
21         }
22         return true;
23     }
24 };

 

Palindrome Number

标签:

原文地址:http://www.cnblogs.com/zl1991/p/4730555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!