Description
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Examples
1 | Input: -1 |
1 | Input: 101 |
1 | Input: 20 |
Pseudocode
Actually, the first thought rising up in my mind was to convert the integer number into string, then compare the characters. However, there is a follow up requirement, which is Coud you solve it without converting the integer to a string?
. Then I thought of another way: get each digit of the integer, store them into a array list, then compare these digits.
Implementation-Java
1 | import java.util.List; |
Improved Implementation
The improved algorithm is from the online discussion. It is a little different from my solution but the general idea is the same. This implement directly calculate the reversed number of the original number, which makes the logic more clearly.
1 | public class Solution{ |