This problem can make use of question 7 - digit reversal
Then compare the reversed number with the original number. If they are equal, it is a palindrome.
Solution 1: Use the previous number inversion
class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}
int t=x;
int ans = 0;
while (x != 0) {
//说明没有移除完
if ((ans * 10) / 10 != ans) {
// 处理过程中如果遇到越界则会返回0;
ans = 0;
break;
}
ans = ans * 10 + x % 10; // 得到反转的整数值
x = x / 10;//移除一位
}
return t == ans;
}
}
Solution 2: For example, java api (recommended in this way)
Use the API provided by StringBuilder and StringBuffer
class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}
String reverseStr = new StringBuilder(String.valueOf(x)).reverse().toString();
return reverseStr.equals(String.valueOf(x));
}
}
Solution two is simple, but requires a certain degree of knowledge. First, you must be familiar with StringBuilder and StringBuffer, and know that there is a reverse method