問題
https://leetcode.com/problems/roman-to-integer/
解いた
# @param {String} s # @return {Integer} def roman_to_int(s) convert_hash = { "I" => 1, "V" => 5, "X" => 10, "L" => 50, "C" => 100, "D" => 500, "M" => 1000 } result = 0 chars = s.chars chars.each_with_index do |char, index| next_index = convert_hash.to_a.find_index { |k| k[0] == chars[index + 1] } current_index = convert_hash.to_a.find_index { |k| k[0] == char} if next_index.nil? || next_index <= current_index result += convert_hash[char] else result -= convert_hash[char] end end p result end
結果