LeetCode题解--9. Palindrome Number

来源:互联网 发布:gprs数据采集器 编辑:程序博客网 时间:2024/06/05 18:42

链接


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 mainbool 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]
0 0
原创粉丝点击