[LeetCode] 063: Palindrome Number

来源:互联网 发布:好看的网络自制剧 编辑:程序博客网 时间:2024/06/14 22:06
[Problem]

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.


[Solution]

class Solution {
public:
bool isPalindrome(int x) {
// Note: The Solution object is instantiated only once and is reused by each test case.

// negative number
if(x < 0)return false;

// reverse
int y = x;
long long z = 0;
while(y != 0){
z = z*10 + y%10;
y /= 10;
}
return x == z;
}
};
说明:版权所有,转载请注明出处。Coder007的博客
原创粉丝点击