Ruby Solution


  • 0
    S
        romans = [["I","V","X","L","C","D","M"],[1,5,10,50,100,500,1000]]
    
        result = 0
        s = s.split("")
        i = 0
        
        while i<s.length
            j = 0
            if (s[i] == "I" && s[i+1] == "X") || (s[i] == "I" && s[i+1] == "V")
                result -= 2
    
            elsif (s[i] == "X" && s[i+1] == "L") || (s[i] == "X" && s[i+1] == "C")
                result -= 20
                
            elsif (s[i] == "C" && s[i+1] == "D") || (s[i] == "C" && s[i+1] == "M")
                result -= 200
            end
            
            while j<romans[0].length
    
                if s[i] == romans[0][j]
                    result += romans[1][j]
                end
                j += 1
            end
            i +=1
        end
        
        return result
    end
    

Log in to reply
 

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