Daily Practice (Palindrome Number in Java and Python 3 )
阿新 • • 發佈:2018-12-13
I wrote this at LeetCode Java
class Solution { public boolean isPalindrome(int x) { if( x<0 || (x%10==0 && x!=0) ) return false; //think about the edge case; int rev=0; while(x>rev){ rev=rev*10+x%10; x/=10; } return x==rev||x==rev/10; //take odd number and even number into consideration. } }
Python 3
class Solution: def isPalindrome(self, x): if x >= 0 and x < 10: return True if x < 0: return False if x % 10 == 0: return False res = [] temp = x while temp > 0: res.append(temp % 10) temp = temp // 10 for i, val in enumerate(res): temp += val * (10 ** (len(res) - i - 1)) return temp == x