Cover image for Classic Interview 150 Questions P9 Palindrome Number

Classic Interview 150 Questions P9 Palindrome Number


Timeline

Timeline

2025-11-26

init

Math

Problem:

to_string, just compare the strings directly

123456789101112131415161718192021
#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;	}};
Loading comments…