leetcode palindrome number

Determines whether an integer is a palindrome. A palindrome is an integer that reads the same in positive order (from left to right) and in reverse order (from right to left).

Example 1:

Input: 121
 Output: true

Example 2:

Input: -121
 Output: false
 Explanation: Read from left to right, it is -121. Reading from right to left, it is 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
 Output: false
 Explanation: Read from right to left, 01. Therefore it is not a palindrome.
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        num =str(x)
        pattern_number = re.compile(r'^\-')
        match_num = pattern_number.match(num)

        if match_num:
            return False
        else:
            num = list(num)
            if len(num)%2==0:
                num_sub_one = num[0:len(num)//2]
                num_sub_two = num[len(num)//2:]
                num_sub_two.reverse()
                if num_sub_one == num_sub_two:
                    return True
                else:
                    return False
            else:
                num_sub_one = num[0:len(num) // 2]
                num_sub_two = num[(len(num)//2+1):]
                num_sub_two.reverse()
                if num_sub_one == num_sub_two:
                    return True
                else:
                    return False

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325120071&siteId=291194637