LeetCode Palindrome Number&&Reverse Integer解法集合


1. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321Example2: 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.9 update:以前は複雑だと思っていましたが、そう処理すべきではありません.更新して、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;
	}