#13 Roman to Integer

Roman numerals are represented by seven different symbols: IVXLCD and M.

 

 

 

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.


Solution1:

class Solution:
  def romanToInt(self, s: str) -> int:
    output = 0
    i = 0
    while(i<len(s)):
      if s[i] == "I":
        if i < len(s)-1 and s[i+1] == "V":
          output += 4
          i += 2
        elif i < len(s)-1 and s[i+1] == "X":
          output += 9
          i += 2
        else:
          output += 1
          i += 1
      elif s[i] == "X":
        if i < len(s)-1 and s[i+1] == "L":
          output += 40
          i += 2
        elif i < len(s)-1 and s[i+1] == "C":
          output += 90
          i += 2
        else:
          output += 10
          i += 1
      elif s[i] == "C":
        if i < len(s)-1 and s[i+1] == "D":
          output += 400
          i += 2
        elif i < len(s)-1 and s[i+1] == "M":
          output += 900
          i += 2
        else:
          output += 100
          i += 1
      elif s[i] == "V":
        output += 5
        i += 1
      elif s[i] == "L":
        output += 50
        i += 1
      elif s[i] == "D":
        output += 500
        i += 1
      elif s[i] == "M":
        output += 1000
        i += 1

    return output

 

Solution2:

class Solution:
  def romanToInt(self, s: str) -> int:
    output = 0
    for i in range(len(s)):
      if s[i] == "I":
        output += 1
      elif s[i] == "V":
        output += 5
      elif s[i] == "X":
        output += 10
      elif s[i] == "L":
        output += 50
      elif s[i] == "C":
        output += 100
      elif s[i] == "D":
        output += 500
      elif s[i] == "M":
        output += 1000

    num_iv = s.count("IV")
    num_ix = s.count("IX")
    num_xl = s.count("XL")
    num_xc = s.count("XC")
    num_cd = s.count("CD")
    num_cm = s.count("CM")

    output -= (num_iv + num_ix) * 2 \
      + (num_xl + num_xc) * 20 \
      + (num_cd + num_cm) * 200


    return output

posted @ 2019-11-11 21:22  MrDoghead  阅读(107)  评论(0编辑  收藏  举报