LeetCode题解--9. Palindrome Number

链接

Leetcode题目: https://leetcode.com/problems/palindrome-number/

Github代码:https://github.com/gatieme/LeetCode/tree/master/009-PalindromeNumber

CSDN题解:http://blog.csdn.net/gatieme/article/details/51046193

题目

检测一个整数是不是回文数字

直接将该整数反序,反序后看是不是等于其本身即可

代码



#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


#define __tmain main


bool isPalindrome(int x)
{
    long long xx = x;
    long long new_xx = 0;

    while (xx > 0)
    {
        new_xx = new_xx * 10 + xx % 10;
        xx /= 10;

    }

    return new_xx == (long long)x;


}

int __tmain(void)
{

    printf("%d", isPalindrome(12321));
    return EXIT_SUCCESS;

}


如果是Python

class Solution(object):
    def isPalindrome(self, x):
        """ :type x: int :rtype: bool """
        return str(x) == str(x)[::-1]

你可能感兴趣的:(LeetCode,算法,面试,回文,palindrome)