标签:style blog io ar color os sp on div
Determine whether an integer is a palindrome. Do this without extra space.
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 // 2 // main.cpp 3 // Longest Substring 4 // 5 // Created by Bowie Hsu on 29/11/21. 6 // Copyright (c) 2014年 Bowie Hsu . All rights reserved. 7 // 8 9 #include <iostream> 10 #include <string> 11 #include <sstream> 12 #include <vector> 13 #include "stdio.h" 14 #include "ctype.h" 15 16 using namespace std; 17 18 class Solution 19 { 20 public: 21 bool isPalindrome(int x) 22 { 23 //记录输入整数的长度 24 if (x<0) { 25 return 0; 26 } 27 int a=1; 28 int p=0; 29 int calc=x; 30 while (calc/10!=0) 31 { 32 a=a*10; 33 calc=calc/10; 34 ++p; 35 } 36 int test=a; 37 int big=x,small=x; 38 bool bign=1; 39 int cha=0; 40 while(small) 41 { 42 if(big/test!=small%10) 43 bign=0; 44 45 cha=big/test; 46 big=big-test*cha; 47 test=test/10; 48 small=small/10; 49 50 } 51 return bign; 52 } 53 }; 54 55 int main() 56 { 57 int input=1211; 58 bool ans; 59 Solution x; 60 ans=x.isPalindrome(input); 61 cout<<ans<<endl; 62 // cout<<"what the fuck?"; 63 }
标签:style blog io ar color os sp on div
原文地址:http://www.cnblogs.com/bowiehsu/p/4133875.html