【算法day9】回文数-给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
例如,121 是回文,而 123 不是。
https://leetcode.cn/problems/palindrome-number/description/
class Solution {
public:
bool isPalindrome(int x) {
int MAX_INT_LENGTH = 12;
int* ch = (int*)calloc(sizeof(int), MAX_INT_LENGTH + 1);
int idx = 0;
if (x < 0) {
ch[idx] = '-';
idx++;
}
while (x != 0) {
int tmp = x % 10;
ch[idx] = tmp + '0';
x = x / 10;
idx++;
}
for (int i = 0; i < idx; i++) {
if (ch[i] != ch[idx - 1 - i]) {
return false;
}
}
return true;
}
};