回文數(shù)
時(shí)間復(fù)雜度O(log??x)
bool isPalindrome(const int x) { if (x < 0) { return false; } if (0 == x) return true; int original = x; int temp = 0; while (original > 0) { temp = temp * 10 + (original % 10); original = original / 10; } return temp == x; } int main() { // 測試用例 cout << isPalindrome(121) << endl; // true cout << isPalindrome(-121) << endl; // false cout << isPalindrome(10) << endl; // false cout << isPalindrome(12321) << endl; // true cout << isPalindrome(0) << endl; // true return 0; }