leetcode 练习题 -- 9. Palindrome Number

描述:

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.

这里的 Some hints 其实就是想告诉你 负数不是回文数

博主这里是把int类型转化为字符串判断了

代码如下:

class Solution {
public:
	bool isPalindrome(int x)
	{
		if (x < 0)return false;
		string s = to_string(x);
		for (int i = 0; i < s.length(); i++)
			if (s[i] != s[s.length() - i - 1])
				return false;
		return true;
	}
};



猜你喜欢

转载自blog.csdn.net/tobealistenner/article/details/79088179
今日推荐