【Leetcode】python - [9] Palindrome Number 個人解法筆記

題目出處

9. Palindrome Number

難度

easy

個人範例程式碼

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False
        if x == 0:
            return True

        return self.is_palindrome(str(x))

    def is_palindrome(self, s):
        start = 0
        end = len(s) - 1
        while(start < end):
            if s[start] == s[end]:
                start += 1
                end -= 1
            else:
                return False

        return True

最近在練習程式碼本身就可以自解釋的 Coding style,可以嘗試直接閱讀程式碼理解

算法說明

先設計好一個判斷 palindrome 的 function,然後再處理特殊狀況

input handling

  • 如果 = 0,回傳 True
  • 如果 < 0,回傳 False

Boundary conditions

用 start, end 來判斷 palindrome

Reference

Licensed under CC BY-NC-SA 4.0