sudopower

lc_9

LC: 9: Palindrome Number

Link to problem

Solution 1

func isPalindrome(x int) bool {
    
    xStr := fmt.Sprintf("%d",x)
    var revXStr string
    for idx:= range xStr{
        revXStr+=string(xStr[len(xStr)-idx-1])
    }

    return xStr==revXStr
}

Solution 2

func isPalindrome(x int) bool {
    if x<0{
        return false
    }

    var nos []int
    for x != 0{
        nos = append(nos,x%10)
        x=x/10

    }

    var i,j int
    j=len(nos)-1
    for i<j{
        if nos[i]!=nos[j]{
            return false
        }
        i++
        j--
    }

    return true
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blogs