leet code Palindrome Number (facile)

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

brute force

class Solution:
    def isPalindrome(self, x: int) -> bool:
        for i in range(len(str(x))):
            x = str(x)
            if x[i] != x[len(str(x))-i-1]:
                return False
        if int(x) < 0:
            return False
        else:
            return True

bit faster solution

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False
        if str(x) != str(x)[::-1]:
            return False
        else:
            return True

Recommended Posts

leet code Palindrome Number (facile)
Commentaire du Leet Code "200. Nombre d'îles"
leet code easy Préfixe commun le plus long
leet code easy Roman to Integer Approach 1: Passage de gauche à droite
[Leet Code Daily Challenge 2020-6-18] H-Index II