Check a positive number is a palindrome or not.
A palindrome number is that if you reverse the whole number you will get exactly the same number.
java
public class Solution {
/*
* @param num: a positive number
* @return: true if it's a palindrome or false
*/
public boolean isPalindrome(int num) {
// write your code here
if (num < 10) {
return true;
}
int rev = reversed(num);
return rev == num;
}
private int reversed(int num) {
int nums = 0;
while (num != 0) {
int start = num % 10;
num /= 10;
nums = nums * 10 + start;
}
return nums;
}
}
python
class Solution:
"""
@param: num: a positive number
@return: true if it's a palindrome or false
"""
def isPalindrome(self, num):
# write your code here
if num < 10:
return True
return self.util(num) == num
def util(self, num):
nums = 0
while num != 0:
start = num % 10
nums = nums * 10 + start
num /= 10
return nums