[PYTHON] leet code Palindrome Number (einfach)

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 (einfach)
Leet Code "200. Anzahl der Inseln" Kommentar
leet code easy Längstes gemeinsames Präfix
leet code easy Roman to Integer Ansatz 1: Pass von links nach rechts
[Leet Code Daily Challenge 2020-6-18] H-Index II