中午去图书馆,有插座的座位都被占了,看我的y7000p能抗多久。。。
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
solution(转为String进行处理吧!)
class Solution {
public boolean isPalindrome(int x) {
String num = Integer.toString(x);
int len = num.length();
for(int left=0,right=len-1;left<right;left++,right--){
if(num.charAt(left)!=num.charAt(right))
return false;
}
return true;
}
}
下面这个答案不用判断是否越界,具体分析可见本人第7题分析 https://blog.csdn.net/lmyaxx/article/details/84895456
class Solution {
public boolean isPalindrome(int x) {
if(x<0)
return false;
return revert(x)==x;
}
public int revert(int x){
int ans=0;
while(x>0){
ans=ans*10 + x%10;
x=x/10;
}
return ans;
}
}