LeetCode[9]Palindrome Number

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/khy19940520/article/details/77995382

Determine whether an integer is a palindrome. Do this without extra space.

本地可执行代码:

#include<iostream>
using namespace std;

/*
解题思路:判断一个数是否是回文数,把该数首尾反转,反转
之后的数和原数相等,则是回文数,否则不是,注意负数不是
回文数,踩了这个坑好多次。。。。。。。。。
*/

bool isPalindrome(int x);

int main()
{
	bool result;
	int a = -2147447412;
	result = isPalindrome(a);
	cout << result << endl;
	system("pause");
	return 0;
}

bool isPalindrome(int x)
{
	int sum = 0;
	int b = x;
	while (x != 0)
	{
		int s = x % 10;
		sum = sum * 10 + s;
		x = x / 10;
	}
	if (sum == x)
	{
		return true;
	}
	else if(b<0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

提交代码:

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


猜你喜欢

转载自blog.csdn.net/khy19940520/article/details/77995382