现在的位置: 首页 > 综合 > 正文

[LeetCode] Reverse Integer、Palindrome Number

2017年12月23日 ⁄ 综合 ⁄ 共 2489字 ⁄ 字号 评论关闭

Reverse digits of an integer.

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

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

下面提到的提示很有启发。

解法1: 用额外的空间,这样比较直接:

class Solution {
public:
    int reverse(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
    if ( x==0 ) return 0;
	bool neg = x<0;
	x=neg?-x:x;
	vector<int> digits;
	while(x)
	{
		digits.push_back(x%10);
		x/=10;
	}
	int ans=0;
	vector<int>::iterator it;
	for( it=digits.begin();it!=digits.end();++it)
		ans=ans*10+*it;
	return neg?-ans:ans;
    }
};

不用额外空间的:

class Solution {
public:
    int reverse(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
    if ( x==0 ) return 0;
	bool neg = x<0;
	x=neg?-x:x;

	int ans=0;
	while(x)
	{
		ans=ans*10+x%10;
		x=x/10;
	}
	return neg?-ans:ans;
    }
};

Palindrome Number:

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.

按提交后的代码来看,负数都不认为是回文。
然后代码中注意两个地方,不能用  while( high*10 <= x )  high*=10 ,来求high,因为这样有可能high*10会溢出,所以要换一个方式。
另外是 x 缩小的时候要 high/=100,而不是/=10。
class Solution {
public:
    bool isPalindrome(int x) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		if ( x==0 ) return true;
        if ( x<0 ) return false;

		int high=1;
		while(x/high>=10)
			high*=10;
		while(x)
		{
			int l=x%10;
			int h=x/high;
			if( l!= h ) 
				return false;
			x=x%high;
			x/=10;
			high/=100;
		}
		return true;
	}
};

First Missing Positive:

Given an unsorted integer array, find the first missing positive integer.

For example,

Given [1,2,0] return 3,

and [3,4,-1,1] return 2.

Your algorithm should run in O(n)
time and uses constant space.

class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        assert(A&&n>=0);
	    if(n==0)
		    return 1;
	    const int INVALID=100000;
	    for(int i=0;i<n;i++)
		    if ( A[i]>n||A[i]<=0)
			    A[i]=INVALID;
	    for(int i=0;i<n;i++)
	    {
		    int trueNum=A[i]<0?-A[i]:A[i];
		    if ( trueNum!=INVALID&&A[trueNum-1]>0)
			    A[trueNum-1]=-A[trueNum-1];
	    }
	    for(int i=0;i<n;i++)
		    if(A[i]>=0)
			    return i+1;
	    return n+1;
    }
};

抱歉!评论已关闭.