Cover image for Interview Classic 150 Questions P9 Palindrome Number

Interview Classic 150 Questions P9 Palindrome Number


Timeline

Timeline

2025-11-26

init

Mathematics

Problem:

to_string just compare strings directly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <algorithm>
using std::string;

class Solution {
public:
bool isPalindrome(int x)
{
if (x < 0) {
return false;
}
string s1 = std::to_string(x);
string s2(s1);
std::reverse(s2.begin(), s2.end());
if (s1.compare(s2) == 0) {
return true;
}
return false;
}
};