LeetCode 9. Palindrome Number
Description
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example
Follow up
Coud you solve it without converting the integer to a string?
Code
- java
class Solution {
public boolean isPalindrome(int x) {
if(x < 0) return false;
int temp = x, reverseSum = 0;
while(temp != 0) {
reverseSum = reverseSum * 10 + temp % 10;
temp /= 10;
}
return x == reverseSum;
}
}
Conclusion
- 取余再累加的过程就是在求一个整数的回文数