LeetCode之回文数

 我的做法是直接将每一位取出来,然后用字符串存储,最后运用c++自带的字符串反转函数,看两个字符串是不是一样。

class Solution {
public:
    bool isPalindrome(int x) {
        string ans = "";
        for( long i = abs((long)x) ; i > 0 ; i /= 10 ){
            ans += char(i % 10);
        }
        if( x < 0 ) ans += '-';
        string flag = ans ;
        reverse( ans.begin() , ans.end() ) ; 
        if( flag == ans ) return true;
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/wuhenglan/article/details/88312279