TreeviewCopyright © aleen42 all right reserved, powered by aleen42
9.回文数
https://leetcode-cn.com/problems/palindrome-number/
Java
class Solution{
public boolean isPalindrome(int x){
final int ans = x;
if(ans < 0){
return false;
}
int res = 0;
while(x != 0){
res = res * 10 + x % 10;
x = x / 10;
}
return res == ans ? true : false;
}
public static void main(String[] args) {
System.out.println(new Solution().isPalindrome(121));
}
}
Python
'''
@Author: Goog Tech
@Date: 2020-07-15 18:31:53
@Description: https://leetcode-cn.com/problems/palindrome-number/
@Reference: https://leetcode-cn.com/problems/palindrome-number/solution/chao-xiang-xi-tu-jie-san-chong-jie-fa-9-hui-wen-sh/
@FilePath: \leetcode-googtech\#9. Palindrome Number\Solution.py
'''
class Solution:
def isPalindrome(self, x):
if x < 0 or (x % 10 == 0 and x != 0):
return False
ans = 0
while x > ans:
ans = ans * 10 + x % 10
x = x // 10
return x == ans or x == (ans // 10)
print(Solution().isPalindrome(121))