Home  >  Article  >  Backend Development  >  How to determine the number of palindromes in Python

How to determine the number of palindromes in Python

coldplay.xixi
coldplay.xixiOriginal
2021-03-09 13:57:0642904browse

How Python determines the palindrome number: First convert the array into a string; then set two pointers, one to traverse the string from left to right, and one to traverse from right to left. If you encounter two different pointers, In the case of equality, it is not a palindrome number until the two pointers are equal.

How to determine the number of palindromes in Python

The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer.

How to determine the number of palindromes in python:

It’s very simple to solve using python

Two pointers, one traverses the string from left to right, One traverses from right to left. If two unequal situations are encountered, it will not be a palindrome until the two pointers are equal

Code implementation

class Solution:
    def isPalindrome(self, x: int) -> bool:
        s=str(x)
        l,r=0,s.__len__()-1
        while(l<=r):
            if s[l]==s[r]:
                l+=1
                r-=1
            else:return False
        return True

Related free learning Recommended: python video tutorial

The above is the detailed content of How to determine the number of palindromes in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn