[Leetcode❤Python] 9. Palindrome Number, leetcodepalindrome

Source: Internet
Author: User

[Leetcodepython] 9. Palindrome Number, leetcodepalindrome

# Number of replies
# Method1: Compare the integer transpose with the original number, which is the same as the return number. The negative number is not the return number.
# Overflow does not need to be considered when the integer is reversed here, but it does not mean that if it is a C/C ++ or other language, it does not need to be considered
Class Solution (object ):
Def isPalindrome (self, x ):
"""
: Type x: int
: Rtype: bool
"""
If x <0: return False
# If the negative number is not the number of replies, return False
Xre = x
Ans = 0
While x> 0:
Ans = ans * 10 + x % 10
X = x // 10

If ans> 21474836547:
Ans = 0
Print ans, xre
Return ans = xre


# Method2
Class Solution (object ):
Def isPalindrome (self, x ):
"""
: Type x: int
: Rtype: bool
"""
If x <0: return False
# If the negative number is not the number of replies, return False
Digits = 1
While x/digits> = 10:
Digits * = 10

While digits> 1:
Right = x % 10
Left = x/digits
If left! = Right: return False
X = (x % digits)/10
Digits // = 100

Return True

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.