LeetCode09. 回文数 [简单]

题目描述:

我的解题:

1.用第七题的方法,将数翻转后与原数比较

class Solution {
public:
    bool isPalindrome(int x) {
        if(x>=0&&x<=9)
            return true;
        if(x<0||x%10==0)
            return false; 
        int temp=x;
        long a=0;
        while(temp!=0){
            a=a*10+temp%10;
            temp/=10;
        }
        return x==a?true:false;
    }
};

2. 转为字符串操

reverse()函数将左闭右开区间内的元素全部逆序

class Solution {
public:
    bool isPalindrome(int x) {
        string a=to_string(x);
        string b=a;
        reverse(b.begin(),b.end());
        return a==b?true:false;
    }
};

发布了15 篇原创文章 · 获赞 0 · 访问量 98

猜你喜欢

转载自blog.csdn.net/qq_41041762/article/details/105097200