题目名称
Palindrome Number—LeetCode链接
描述
Determine whether an integer is a palindrome. Do this without extra space.
分析
判断一个数是否为回文数。回文数
我才用的方法是将整数转换成字符串,利用上一篇文章中讲到的C++字符串流<sstream>。设定一前一后指针,分别指向字符串的头尾,如果不同则返回false,指针向中间逐位移动。
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
bool isPalindrome(int x) {
stringstream istream;
string num_str;
istream<<x;
istream>>num_str;
int num_length = num_str.size();
for(int i=0,j=num_length-1;i<j;i++,j--){
if(num_str[i]!=num_str[j])
return false;
}
return true;
}
int main(){
int a=10;
cout<<"Is "<<a<<" a Palindrome Number?"<<isPalindrome(a)<<endl;
}