9. Palindrome Number
Given an integer x, return true if x is a palindrome, and false otherwise.
Example 1:
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
Follow up: Could you solve it without converting the integer to a string?
class Solution {
public:
bool isPalindrome(int x) {
if ((x < 0) || ((x % 10 == 0) && (x != 0))) return false;
string s = to_string(x);
int n = s.size();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - i - 1]) {
return false; // ❌ chỉ cần 1 cặp sai là không phải palindrome
}
}
return true; // ✅ chỉ return true khi duyệt hết mà không sai cặp nào
}
};