Leetcode回文数

直接上python代码

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x<0:                            //负数必不是回文数
            return 1==0
        sws = []
        for i in str(x):                   //将x反转然后判断是否相等
            sws.append(i)
        sws.reverse()
        sws = ''.join(sws)
        return x == int(sws)
class Solution:
    def isPalindrome(self, x: int) -> bool:
        s = list(str(x))
        t = s[::-1]      //暴力取反
        return t==s

C语言

bool isPalindrome(int x){
if (x < 0 || (x % 10 == 0 && x != 0))  //小于0,最后一位数为0都不是回文数
            return false;
int n=0,x_0=x,i=0;
while(x)
{
    if(i == 9 && (x_0%10)>2)        //溢出的也不是回文数
        return false;
    n = n*10 + x%10;               //对x取反
    x = x/10;
    i += 1;
}
return n == x_0;
}

题目来源:9. 回文数 - 力扣(LeetCode) (leetcode-cn.com)

posted @ 2022-10-26 18:02  放氮气的蜗牛  阅读(8)  评论(0)    收藏  举报  来源