您的位置:首页 > 其它

【LeetCode】LeetCode——第9题:Palindrome Number

2016-04-19 10:31 411 查看


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

题目的大概意思是:判断一个整型(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: