Determine whether an integer is palindrome
Title Description:
Source: LeetCode 09 palindromes
Give you an integer x, if x is a palindrome integer, return true; Otherwise, return false.
Palindrome numbers refer to integers whose positive order (left-to-right) and reverse order (right-to-left) are read the same. For example, 121 is palindrome and 123 is not.
Example 1:
Input: x = 121
Output:true
Example 2:
Input: x = -121
Output: false
Explanation: Read from left to right, -121. Read from right to left, 121-. So it's not a palindrome number.
Example 3:
Input: x = 10
Output: false
Explanation: Read from right to left, 01. So it's not a palindrome number.
Example 4:
Input: x = -101
Output: false
Advanced:
Can you solve this problem by converting integers to strings?
My Answer:
class Solution: def isPalindrome(self, x: int) -> bool: str1 = str(x) flag = True for i in range(0, int(len(str1)/2)): if str1[i] != str1[len(str1) - 1 -i]: flag = False break return flag
Execution Result: Passed
Execution time: 56 ms, beating 75.91% of all Python 3 submissions
Memory consumption: 14.7 MB, beating 99.20% of all Python 3 submissions
Pass test case: 11510 / 11510
Advanced:
My Answer:
Idea: first find the number of integers, then divide this number by 10 through two for loops, then receive the high and high position numbers with end and start, and then compare if the start and end are equal, otherwise they are not palindromes
class Solution: def isPalindrome(self, x: int) -> bool: count = 0 flag = True number = x number1 = number if number1 < 0: flag = False while number1 > 0: count += 1 number1 = int(number1/10) for i in range(0,count): end = number start = number #Get the number from the lowest bit in turn after the remainder operation for j in range(0,i): end = int(end/10) #Get the number from the highest bit in turn after the remainder operation for k in range(0,count - 1 - i): start = int(start / 10) if (start % 10) != (end % 10): flag = False break return flag
Improvement:
Think: Use a while loop, divide by 10 and then take the rest back_number receives the inverse number of x, and finally determines if the inverse number is equal to the previous number
class Solution: def isPalindrome(self, x: int) -> bool: back_number = 0 number1 = number2 = x #If x is negative, then it must not be a loopback number if x < 0: return False #Use back_by polling for dividing by 10 Number receives the inverse number of x while number1 > 0: back_number = (back_number * 10) + (number1 % 10) number1 = int(number1 / 10) return x == back_number
Big man answer:
Idea: Is x >=0 equal to X after reverse order (list used)
(This submission defeated more than 60% of users, but it was the fastest I could find. None of the answers in the python area seems to be fast. That might be the reason for python. If any of the guys have a faster algorithm, please comment and tell me, thank you guys!)
class Solution: def isPalindrome(self, x: int) -> bool: return x>=0 and int(str(x)[::-1]) == x
[: -1] Explanation: (Big man is 6)
b = a[i:j] # Represents copying a[i] to a[j-1] to generate a new list object a = [0,1,2,3,4,5,6,7,8,9] b = a[1:3] # [1,2] #When i defaults, the default is 0, that is, a[:3] equals a[0:3] #When j defaults, the default is len(alist), which means a[1:] corresponds to a[1:10] #When I and j are both default, a[:] is equivalent to a full copy of a b = a[i:j:s] # Representation: i,j i s the same as above, but s i s a step, defaulting to 1. therefore a[i:j:1]Amount to a[i:j] #When s<0, i defaults to -1. j, and -len(a)-1 #So a [:-1] corresponds to a[-1:-len(a)-1:-1], that is, to copy from the last element to the first element once, in reverse order.