2022年9月28日 星期三

Leetcode 紀錄 - Palindrome Number

 Palindrome Number

Given an integer x, return true if x is palindrome integer.

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

For example, 121 is a palindrome while 123 is not.

這個問題是說會輸入一個數值,如果是一個對稱的數就回傳 true ,不是對稱的就還傳 false 。

我自己的處理辦法是將輸入的數值轉成字串處理XD (超懶 應該有更棒的處理辦法就是了

然後直接透過 for 加上 python 可以用負值的 index 去倒著讀取字串。

就可以直接拿字串第一個和最後一個做 sequence XD

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

x=12521
print(Solution.isPalindrome(Solution,x))

沒有留言:

張貼留言