//解题思路,回文数就是1234321
//分别提取最高位和最低位的数进行比较
#include <iostream>
using namespace std;
这个处理不了溢出
//class Solution{
//public:
// bool isParlindrome(int x){
// int nTen = 1;
// int high = 0;
// int low = 0;
// if(x == 0){
// return true;
// }
// if(x == 1){
// return true;
// }
// while(x>nTen){
// nTen = nTen * 10;
// }
// nTen = nTen / 10;
// while(x/10 > 0){
// high = x / nTen;
// low = x % 10;
// if(high != low){
// return false;
// }
// x = x - nTen * high;
// nTen = nTen / 100;
// x = x / 10;
// }
// return true;
// }
//};
//这个可以处理溢出
//解题思路:一个回文数的逆序输出和原数相等
class Solution{
public:
bool isParlindrome(int x){
int min = 0;
int max = x;
while(max > 0){
min *= 10;
min += max % 10;
max /= 10;
}
return min == x;
}
};
int main()
{
int x;
bool tORf;
Solution s;
cout << "Enter a world: " << endl;
cin>>x;
tORf = s.isParlindrome(x);
if(tORf == true){
cout<<"true"<<endl;
}
else{
cout<<"false"<<endl;
}
while(1);
return 0;
}
Leetcode Parlindrome Number 9
最新推荐文章于 2018-12-10 22:15:34 发布