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

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

你可能感兴趣的:(LeetCode,number,palindrome)