9. Palindrome Number, palindrome

Source: Internet
Author: User

9. Palindrome Number, palindrome

Determine whether an integer is a palindrome. Do this without extra space.

 

Some hints:

Cocould negative integers be palindromes? (Ie,-1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You cocould also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How wocould you handle such case?

There is a more generic way of solving this problem.

 

 

 
 1 class Solution: 2     def isPalindrome(self, x): 3         """ 4         :type x: int 5         :rtype: bool 6         """ 7         if x < 0: 8             return False 9         elif x < 10:10             return True11         t = x12         n = 013         while t > 0:14             n += 115             t = t // 1016         for i in range(n // 2):17             lnum = (x // (10 ** (n-i-1))) % 1018             rnum = (x // (10 ** i) ) % 1019             if lnum != rnum:20                 return False21         return True

Determines whether the two ends are equal based on the number of digits.

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.