问题链接:https://leetcode.com/problems/palindrome-number/#/description
Question:Determine whether an integer is a palindrome. Do this without extra space.
题意:判断一个数是否是回文数,不使用额外的空间。
分析:直接算出数字的倒序与该数字比较即可。
代码:
C++版
class Solution { public: bool isPalindrome(int x) { int newNumber = 0; int originalNumber = x; if(x < 0 ){ return false; } while(originalNumber > 0){ newNumber = originalNumber%10 + newNumber*10; originalNumber /= 10; } if(newNumber == x){ return true; }else{ return false; } } };
Python版:
class Solution: # @return a boolean def isPalindrome(self, x): return str(x) == str(x)[::-1]
时间: 2024-12-14 21:22:35