`
bcyy
  • 浏览: 1809678 次
文章分类
社区版块
存档分类
最新评论

LeetCode Palindrome Number && Reverse Integer 解法集合

 
阅读更多

1. Reverse Integer

Reverse digits of an integer.

Example1:x = 123, return 321
Example2:x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

int reverse(int x) {  
		int result = 0;  

		while (x)  
		{  
			result = result*10 + x%10;  
			x /= 10;  
		}  

		return result;  
	}  

12.9update:之前想复杂了,不应该那样处理,更新一下,使用long long就好了,如下程序:

考虑:

1 溢出

2 + -

3 1000, 10等尾部为零的情况

答案:

1 long long 处理了

2 无需要处理

3 无需要处理

int reverse(int x)
{
	long long res = 0;
	while (x)
	{
		res = res*10 + x%10;
		x /= 10;
	}
	if (res >= INT_MAX) return INT_MAX;
	if (res <= INT_MIN) return INT_MIN;
	return res;
}


2 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.

去掉最高位额外处理可能溢出的情况,然后转置整数比较的程序:

class Solution {
public:
	bool isPalindrome(int x) {
		if(x<0) return false;
		else if(x<10) return true;
		int y = 0;
		int a = x%10;
		x /= 10;
		while (x >= 10)
		{
			if(x%10 != 0)
				y = 10*y + x%10;
			x/=10;
		}
		if(a != x) return false;
		x=y;
		int z = 0;
		while (y)
		{
			z = 10*z + y%10;
			y/=10;
		}
		if(z != x) return false;
		return true;
	}
};


最高位和最小位分别比较的程序:

bool isPalindrome(int x) {
		if (x < 0) return false;
		int div = 1;
		while (x / div >= 10) {
			div *= 10;
		}        
		while (x != 0) {
			int l = x / div;
			int r = x % 10;
			if (l != r) return false;
			x = (x % div) / 10;
			div /= 100;
		}
		return true;
	}


另辟蹊径递归程序:

bool isPalindrome(int x, int &y) {
		if (x < 0) return false;
		if (x == 0) return true;
		if (isPalindrome(x/10, y) && (x%10 == y%10)) {
			y /= 10;
			return true;
		} else {
			return false;
		}
	}
	bool isPalindrome(int x) {
		return isPalindrome(x, x);
	}


截开高位数和低位数的程序:

bool isPalindrome(int x) {
		if(x < 0) return false;
		int a = x, b = 0;
		while(a > b) {
			b = b * 10 + a % 10;
			a /= 10;
		}
		if(a == 0) return (x == b);
		return (a == b) || a == (b / 10);
	}


部分程序出处:LeetCode论坛

//2014-1-24 update
class Solution124 
{
public:
	bool isPalindrome(int x) 
	{
		int y = 0;
		if (x<0) return false;
		if (x<10) return true;
		if (x%10 ==0 && x%10%10 == 0) return false;
		while (x > y)
		{
			y = y*10 + x%10;
			x /= 10;
		}
		if (x == y || x == y/10) return true;
		return false;
	}
};

bool isPalindrome(int x) 
	{
		if (x<0) return false;//特殊情况1
		if (x<10) return true;//特殊情况2
		if (x%10 == 0) return false;//特殊情况3:尾数为零的肯定不是Palindrome
		int y = 0;
		while (x > y)
		{
			y = y*10 + x%10;
			x /= 10;
		}
		return x == y || x == y/10;
	}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics