您的位置:首页 > 职场人生

LeetCode题解--9. Palindrome Number

2016-04-02 23:35 363 查看

链接

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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息