Short Ruby solution with O(n)


#1
def romanToInt(a)
    r = 0
    h = { 'M'=>1000, 'D'=>500, 'C'=>100, 'L'=>50, 'X'=>10, 'V'=>5, 'I'=>1 }
    
    a.chars.each.with_index do |c, i|
        if (i < a.length - 1) && h[c] < h[a[i+1]]
            r -= h[c]
        else
            r += h[c]
        end
    end
    
    r
end