当前位置:网站首页>leetcode-2. Palindrome judgment

leetcode-2. Palindrome judgment

2022-07-06 01:38:00 East invincible is me

Give you an integer x , If x Is a palindrome integer , return true ; otherwise , return false .

Palindrome number refers to positive order ( From left to right ) Reverse order ( From right to left ) Read all the same integers .

for example ,121 It's palindrome. , and 123 No .

My original idea of this question is based on java Of , Turn a string into an array , Then cut it in half and compare it before and after , use python It's really troublesome to write . Look at the ideas given in the solution

class Solution:
    def isPalindrome(self, x: int):
        return (str(x)[::-1]==str(x))

take int To str, Flip the string by slicing , Then compare with the original string . It's a very simple way .
# Method 2 reversed function

#print("".join(reversed(a))==a)
#print(''.join(reversed(a))) reversed(a) What you get is a list  join List get string 
#. join():  Connection string array . The string 、 Tuples 、 Elements in the list with the specified characters ( Separator ) The connection generates a new string 

Use reversed function , Flip the string , And then through join Method , Convert the list to a string , Another comparison .

原网站

版权声明
本文为[East invincible is me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060136260975.html