[leetcode题解]9. Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121 Output: true

Example 2:

Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
最简单的方式,直接转为字符串然后判断一下即可。

#define STR_LEN_MAX 64

bool isPalindrome(int x) {
    char s[STR_LEN_MAX + 1];
    int len = 0;
    int i = 0;
    
    if (x < 0)
        return false;
    
    snprintf(s, STR_LEN_MAX, "%d", x);
    len = strlen(s);
    for (i = 0; i < len / 2; i++) {
        if (s[i] != s[len - 1 - i])
            return false;
    }
    return true;
}

本文遵从CC3.0协议转载请注明:转自凌风技术站

本文标题:[leetcode题解]9. Palindrome Number

本文链接地址:http://www.iaccepted.net/algorithm/leetcode/186.html

相关文章