LeetCode9——Palindrome Number

  回文数字,简单处理。将数字各位取出,然后用临时变量累加,当累加完成后这个数字等于原来的数字即是回文数。需要注意的是负数不是回文数。

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

  除开回文数字,回文字符该怎么处理。稍安勿躁,肯定会遇到。初步设想是先取得中间,然后从两侧扩展,如果都相同就为回文字符串。核心应该是str[half] = str[length - half + 1];等遇到了再说。

原文地址:https://www.cnblogs.com/thewaytomakemiracle/p/5027148.html