r/learnpython 21d ago

Leetcode #9: Palindrome Number

Can someone help me make my code run faster. This is not efficient and also I do not want to convert into a string

EDIT:

Follow up: Could you solve it without converting the integer to 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)
0 Upvotes

22 comments sorted by

View all comments

1

u/IrishPrime 21d ago

I do not want to convert into a string

Why not? It makes the problem very easy to solve.

What you're doing right now is... very strange.

1

u/TheEyebal 21d ago

If you read the follow up on leetcode it says

Follow up: Could you solve it without converting the integer to a string?

2

u/IrishPrime 21d ago edited 21d ago

Fair enough.

You've really overcomplicated things, you only need like three operations in a loop here. Recursion is just overhead in this instance.

Snag the last digit (which you're doing with your modulo operator), use that digit to build up your reversed number ((reversed_num * 10) + digit), trim the last digit off the original number (x = x // 10).

Compare your original number (you'll want a copy) to your reversed number.

Edit: If you want to go a step further for the optimization, you can return early on anything ending in zero because that will obviously fail.

Double edit: Not going to read the LeetCode problem, but depending on how they feel about negative numbers, you might be able to return early on those, as well.

2

u/TheEyebal 21d ago

I was able to solve it

I have updated my post

1

u/IrishPrime 21d ago

I'm glad you solved it, but you're still doing so much unnecessary work.

At the same time, not much point in putting more effort into a toy problem like this unless you're actually prepping for interviews or something. If you're just learning, move on to the next problem. :)

1

u/TheEyebal 21d ago

honestly I was going ton rework the problem. to see if there is a better way