【LeetCode】LeetCode——第9題:Palindrome Number
1234 ワード
9. Palindrome Number
My Submissions
Question Editorial Solution
Total Accepted: 118962
Total Submissions: 377213
Difficulty: Easy
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
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.
Subscribe to see which companies asked this question
Show Tags
Show Similar Problems
1つの整数(int)数が回文数であるか否かを判断する.
この問題の難易度レベル:簡単
反転した数が元の数と等しいかどうかを判断する.
反転後の数がオーバーフローする可能性があることに注意してください.なお、負数は回文数ではありません.
コードは次のとおりです.
コードを提出した後、順調にACを落として、Runtime:76 ms.
My Submissions
Question Editorial Solution
Total Accepted: 118962
Total Submissions: 377213
Difficulty: Easy
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
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.
Subscribe to see which companies asked this question
Show Tags
Show Similar Problems
1つの整数(int)数が回文数であるか否かを判断する.
この問題の難易度レベル:簡単
反転した数が元の数と等しいかどうかを判断する.
反転後の数がオーバーフローする可能性があることに注意してください.なお、負数は回文数ではありません.
コードは次のとおりです.
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0){return false;}
int tmp = x;
long y = 0;
while(tmp){
y = y * 10 + tmp % 10;
tmp /= 10;
}
return x == y;
}
};
コードを提出した後、順調にACを落として、Runtime:76 ms.