Leetcode #9 Palindrome Number

Source: https://leetcode.com/problems/palindrome-number/

9. Palindrome Number

Determine whether an integer is apalindrome. 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:

According to the description of this problem, extra space is not allowed. However, I haven’t figured out an effective solution where even a single extra integer is not used. Thus,I allowed myself to add some variables here to solve this problem. The aim, of course, is to use as few variables as possible.

Obviously, we can translate the target into a string, so that we may determine whether the target x here is a palindrome number or not by comparing the corresponding digit.However, this solution will require a char array with n char variables, where n indicates the number of digits of the integer x. This solution, however, is intolerable because it requires too much extra space.

As the hints above point out, we may reverse the integer to see whether it equals to its origin. In this case, we need to pay much attention to whether its reverse overflows. I have done similar job in the solution of problem 7, where I apply an extra integer variable and a long variable. The cost is still not acceptable enough.

What about using two integers? I have thought this through and found a solution that appears “perfect”(As it turns out later, this solution is not accepted). Here I paste the original version of my code which is not adapted to be compiled in leetcode:

#include <iostream>
using namespace std;

int main()
{
	int x,y,z=0;
	cin>>x;
	if (x<0)
	{
		cout<<0<<endl;
		return 0;
	}
	y=x;

	while (x)
	{
		if (y/10==0)
		{
			if (y==x%10)
			{
				while (z--)
				{
					y=y*10;
				}
				x=x-y;
				x=x/10;
				y=x;
				z=0;
				continue;
			}
			else
			{
				cout<<0<<endl;
				return 0;
			}
		}
		else
		{
			y=y/10;
			z++;
		}
	}
	cout<<1<<endl;
	return 0;
}

In this solution, I used two integers: y as the copy of x and z recording the number of digits of x. I compare the first and the last digit of number of the target integer (step 1). If they are equal, then it could be a palindrome number. Then I wipe out the first digit and the last digit to form a new “x”(step2). We may keep moving from step 1 to step 2 until x turns 0.

       However,there is a fatal error. In the case of x = 10021, if we wipe out the first and the last digit, x turns 2. 2 will be identified to be a palindrome number. The problem here is that we ignore the two digits of 0. We expect that the number of digits of x decreases by 2 after step 2, however, in the case that there are some digits of 0 inside the integer, it may decrease by more than 2, which causes the bug.

       Thus,I have considered to have one more variable zz to record the number of digits of x from the former step. If zz minus z is greater than 2, then we need to handle the case that we ignore some digits of 0. We may test the last several (accurately,zz-z-2) digit(s) of x to see whether they are 0. Then we may solve this problem with three extra integer variables. The code may simply be adapted from the code given above.

       Is there a more effective way to solve the problem? Actually, we do not need to have both y (representing x, then recording the top digit) and z (recording the number of digits of have. To achieve these goals, we simply need the variable,and let’s call it z. . n is the number of digits of x. Note that we do not need to apply extra space for n, as the full adapted code shown below:

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

Thanks to Xue Yuan, my roommate, who offers me such a brilliant idea. He claimed that he had got a better solution with only one extra integer variable and shared it with me. We discussed it over and prove that it actually works and does be a better solution. I consider it to be brilliant because the z here serves to get the highest digit of x and indicates the number of digits of x. Compared with the former solution, it focuses more on the media (z) instead of the origin (x). See this solution also in Xue Yuan’s blog: http://blog.csdn.net/zhengzaishen. 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值