Skip to content

Palindromic Numbers

Table of Contents

9. Palindrome Number

  • LeetCode | LeetCode CH (Easy)

  • Tags: math

  • Return true if the given number is a palindrome. Otherwise, return false.
9. Palindrome Number - Python Solution
# Reverse
def isPalindromeReverse(x: int) -> bool:
    if x < 0:
        return False

    return str(x) == str(x)[::-1]


# Left Right Pointers
def isPalindromeLR(x: int) -> bool:
    if x < 0:
        return False

    x = list(str(x))  # 121 -> ['1', '2', '1']

    left, right = 0, len(x) - 1

    while left < right:
        if x[left] != x[right]:
            return False
        left += 1
        right -= 1

    return True


# |------------|------- |---------|
# |  Approach  |  Time  |  Space  |
# |------------|--------|---------|
# |  Reverse   |  O(N)  |   O(N)  |
# | Left Right |  O(N)  |   O(1)  |
# |------------|--------|---------|


x = 121
print(isPalindromeReverse(x))  # True
print(isPalindromeLR(x))  # True

2396. Strictly Palindromic Number

2217. Find Palindrome With Fixed Length

866. Prime Palindrome

2967. Minimum Cost to Make Array Equalindromic

906. Super Palindromes

2081. Sum of k-Mirror Numbers

3260. Find the Largest Palindrome Divisible by K

3272. Find the Count of Good Integers

564. Find the Closest Palindrome

479. Largest Palindrome Product

Comments