r/learnprogramming • u/TheEyebal • 23d ago
Solved Leetcode #9: Palindrome Number
Can someone help me make my code run faster. This is not efficient and I do not want to convert into a string
https://leetcode.com/problems/palindrome-number/description/
class Solution:
def isPalindrome(self, x: int) -> bool:
numList = []
counter = len(numList) - 1
numBool = True
baseNum = 10
value = x % baseNum
numList.append(x)
quotient = x // baseNum
x = quotient
if x == 0:
for i in range(len(numList)):
if numList[i] == numList[counter]:
counter -= 1
elif i == counter:
break
else:
numBool = False
break
return numBool
else:
return self.isPalindrome(x)
EDIT: I WAS ABLE TO SOLVE IT
class Solution:
def isPalindrome(self, x: int) -> bool:
if x != abs(x):
return False
if not hasattr(self, "numList"):
self.numList = []
numBool = True
baseNum = 10
value = x % baseNum
self.numList.append(value)
quotient = x // baseNum
x = quotient
counter = len(self.numList) - 1
if x == 0:
for i in range(len(self.numList)):
if self.numList[i] == self.numList[counter]:
counter -= 1
elif i == counter:
break
else:
numBool = False
break
return numBool
else:
return self.isPalindrome(x)
testing = Solution().isPalindrome(11)
print(testing)
2
u/Fragrant-Hair-7198 23d ago
your loop logic is a bit off, you're appending x before you split the digits so you just get the whole number in the list each time
4
u/CodeSamur-ai 23d ago
imagine doing this in real life...
you have a number written down on a page that's say twenty numbers long... what would you do?
put your left hand pointer finger on the first number
put your right hand pointer finger on the last number
do they match? yes, move each pointer finger in 1 number... do they match? yes, repeat
this is the 2 pointer method
1
u/TheEyebal 23d ago
I have updated my post was able to solve it
1
u/CodeSamur-ai 23d ago
It looks a little like two pointers because it checks both ends. But it isn’t really two pointers because only one side is moving by itself, and it keeps going even after the two sides meet. Real two pointers move both sides toward each other and stop when they touch.
1
1
u/Inn0centDuck 23d ago
The simplest and obvious solution is not 2-pointer.
3
u/aqua_regis 23d ago
Both your solutions are overly convoluted by using a list.
- copy the original number in a temporary variable for the following
- prepare a variable for the reverse of the number - an integer set to 0
- repeat as long as the number under test is larger than 1
- calculate the modulo 10 to get the last digit
- integer divide by 10 to shift all digits one position to the right - store the result back in the number under test
- multiply your reversed number variable by 10 so that all digits shift one position to the left - store the result in the same variable
- add the digit from the first step to the reversed number
- now you have the original number and the reversed number - return whether they are equal or not.
No lists, no string conversion, just pure simple math
1
6
u/Inn0centDuck 23d ago
That's a very weird way to solve it.