Python, Direct solution "no loops"


  • 0
    O

    Convert to string and back to int

    class Solution(object):
    def isPalindrome(self, x):
    """
    :type x: int
    :rtype: bool
    """
    "x=456654"

        if (x<0):
            return (False)
        elif ((x<10) and (x>=0)):
            return (True)
            
        s=str(x)
        ls=len(s)
        Ml=ls%2
        lm=ls//2
        num1=int(s[0:lm])
        sr=s[(lm+Ml):ls]
        num2=int(sr[::-1])
        
        "print(num1)"
        "print(num2)"
        
        if (num1==num2):
            return(True)
        else :
            return (False)

  • 1
    M

    Converting to a string takes extra space, which is prohibited by the problem.


Log in to reply
 

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