classSolution { public: boolisPalindrome(int x){ if (x < 0) returnfalse; longlong ans = 0; longlong raw = x; while (x) { ans = ans * 10 + x % 10; x /= 10; } return raw == ans; } };
2.字符串反转
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: boolisPalindrome(int x){ if (x < 0) returnfalse; string raw = to_string(x); string ans = raw; for (int i = 0, j = raw.length() - 1; i < j; ++i, --j) { char c = ans[i]; ans[i] = ans[j]; ans[j] = c; } return raw == ans; } };