Ruby Solution


  • 0
    V

    Here's a 155 ms runtime Ruby solution! Not sure if it's the best, but I just wanted to contribute and help the Ruby community.

    ROM_NUMS = {
        "I" => 1,
        "V" => 5,
        "X" => 10,
        "L" => 50,
        "C" => 100,
        "D" => 500,
        "M" => 1000
        }
    
    def roman_to_int(s)
        res = 0
        temp = 0
        s.chars.each_with_index do |el, i|
            if ROM_NUMS[s[i + 1]] && ROM_NUMS[el] < ROM_NUMS[s[i+1]]
                temp = ROM_NUMS[el]
            else
                res += (ROM_NUMS[el] - temp)
                temp = 0
            end
        end
        res
    end
    

Log in to reply
 

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