[Power button - small daily practice] 9. palindrome (python)

9. palindrome

Topic links: https://leetcode-cn.com/problems/palindrome-number/
Difficulty: Easy

Title Description

Determine whether an integer is a palindrome. Palindrome correct order (from left to right) and reverse (right to left) reading is the same integer.

Examples

Example 1:
Input: 121
Output: to true

Example 2:
Input: -121
Output: to false
interpretation: read from left to right, -121. Reading from right to left, as 121-. So it is not a palindrome number.

Example 3:
Input: 10
Output: to false
interpretation: from right to left read, 01. So it is not a palindrome number.

Code

class Solution:
    def isPalindrome(self, x: int) -> bool:
        # 法一:一行代码搞定
        # return True if str(x)==str(x)[::-1] else False

        # 法二:
        front , back = 0, 1
        x=str(x)
        while front <= len(x)-back:
             
            if x[front] != x[-back]:
                return False
            front += 1
            back += 1
        return True
Published 44 original articles · won praise 5 · Views 4465

Guess you like

Origin blog.csdn.net/ljb0077/article/details/104728987