Can I to convert int to list of string??


  • 0
    Z

    Is this a valid solution?? it got passed I am just asking. Thanks

    class Solution:
    # @return an integer
    def reverse(self, x):
        isNeg = False
        data = x
        if data < 0:
            isNeg = True
            data = -data
        
        # convert int to string then list then reverse the list 
        list1 = list(str(data))
        list1.reverse()
        
        # get the reversed int
        reverseInt = int(''.join(list1))
        
        if isNeg:
            return -reverseInt
        else:
            return reverseInt

  • 0
    S

    If I were an interviewer, I would prefer to implement the reverse function by yourself.

    However, python always makes problem easier.


  • 0
    Q

    Why we need self in this function? it seems never used?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.