leet code Palindrome Number (easy)

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 (easy)
Leet Code "200. Number of Islands" commentary
leet code easy Longest Common Prefix
leet code easy Roman to Integer Approach 1: Left-to-Right Pass
[Leet Code Daily Challenge 2020-6-18] H-Index II