(java)leetcode-9
1059 ワード
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
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.
問題解決の考え方:
余剰を絶えず除去して元の数の逆転結果を得て、正しいかどうかを比較すればいいのではないでしょうか.
注意:
負数は回文数ではありません.
1つの数が10の倍数である場合、この数は必ず回文数ではありません.△これはほかの人から見たもので、最初はこれとは思わなかった.
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
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.
問題解決の考え方:
余剰を絶えず除去して元の数の逆転結果を得て、正しいかどうかを比較すればいいのではないでしょうか.
注意:
負数は回文数ではありません.
1つの数が10の倍数である場合、この数は必ず回文数ではありません.△これはほかの人から見たもので、最初はこれとは思わなかった.
public class Solution {
public boolean isPalindrome(int x) {
if(x<0 || (x != 0 && x%10 == 0))
return false;
int result = 0,num = x;
while(num != 0)
{
int a = num%10;
result = result*10 +a;
num = num/10;
}
//System.out.println(result);
if(result == x)
return true;
else
return false;
}
}