首页 » 编程之美 » 正文

[leetcode_9]Palindrome Number

这个题简单,判断一个int是否是回文。
需要注意的是 负数不是回文。
最后就将int转为字符串直接判断就好。
一次AC。

class Solution {
public:
    bool isPalindrome(int x) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(x < 0)
            return false;
        char s[100];
        int index = 0;
        while(x > 0)
        {
            s[index++] = x%10;
            x /= 10;
        }
        int i = 0;
        int j = index-1;
        while(i <= j)
        {
            if(s[i] != s[j])
                return false;
            i++;
            j--;
        }
        return true;
    }
};

发表评论