simple ruby implementation with a loop


  • 0
    T
    # @param {Integer} x
    # @return {Boolean}
    def is_palindrome(x)
        return false if x < 0
        return true if x < 10
        digits = Math.log(x, 10).to_i
        (0..digits/2).each do |exp1|
            exp2 = digits - exp1
            num1 = (x/(10**exp1)%10).to_i
            num2 = (x/(10**exp2)%10).to_i
            return false if num1 != num2
        end
        return true
    end
    

Log in to reply
 

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