9. Palindrome Number
- Total Accepted: 159681
- Total Submissions: 475665
- Difficulty: Easy
- Contributors: Admin
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
回文数的判定,首先,负数不是回文数,做法分两种,一种是辗转相除取逆序,一种转换成字符串。因为Java中有API,我就直接转换成字符串做了。
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
代码如下:
public class Solution {
public boolean isPalindrome(int x) {
if (x<0) {
return false;
}
String s = x+"";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)!=s.charAt(s.length()-i-1)) {
return false;
}
}
return true;
}
}