---
# System prepended metadata

title: 13.Roman to Integer
tags: [Leetcode, '2021']

---

---
title: 13.Roman to Integer
tags: Leetcode,2021
---
# 【LeetCode】 13. Roman to Integer
## Description
>Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

>Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000
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.

>羅馬數字用七個符號來表示數字：I、V、X、L、C、D和M。
分別對應到的數字請參照上方。
舉例來說，“二"在羅馬數字中我們會寫成"II”，代表兩個一相加；“十二"則是寫成"XII”，也就是X + II；“二十七"則是寫成"XXVII”，代表XX + V + II。

>羅馬數字通常從大到小會左到右的順序撰寫，然而數字"四"卻不是寫成"IIII"，取而代之的是"IV"，一在五之前代表的是五要減掉一。同樣的，數字九會寫成IX。我們直接列出六種所有可能的減法情形給你：

>I可以放在V(5)和X(10)前面來完成4和9。
X可以放在L(50)和C(100)前面來完成40和90。
C可以放在D(500)和M(1000)前面來完成400和900。
給予一個羅馬數字，請將它轉換為一般整數。數字大小只會在1到3999之間。
## Example:
```
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.
```
## Solution
```
public class Solution {
    public int RomanToInt(string s) {
        int convertedNumber = 0;
int counter = 0;
Dictionary<char, int> romanDictionary = new Dictionary<char, int>()
{
{ 'I', 1 },
{ 'V', 5 },
{ 'X', 10 },
{ 'L', 50 },
{ 'C', 100 },
{ 'D', 500 },
{ 'M', 1000 }
};

        while (counter < s.Length)
        {
            if (((counter + 1) < s.Length) &&
                (romanDictionary[s[counter]] > romanDictionary[s[counter+1]]))
            {
                convertedNumber += romanDictionary[s[counter]];
                counter++;
            }
            else if(((counter + 1) < s.Length) &&
                (romanDictionary[s[counter]] < romanDictionary[s[counter + 1]]))
            {
                convertedNumber += romanDictionary[s[counter + 1]] - romanDictionary[s[counter]];
                counter += 2;
            }
            else
            {
                convertedNumber += romanDictionary[s[counter]];
                counter++;
            }                
        }

        return convertedNumber;
    }
        
    }
```
```
public class Solution {
   public int RomanToInt(string s) {
			Dictionary<char, int> rn = new Dictionary<char, int>();
			rn.Add('I', 1);
			rn.Add('V', 5);
			rn.Add('X', 10);
			rn.Add('L', 50);
			rn.Add('C', 100);
			rn.Add('D', 500);
			rn.Add('M', 1000);

			int sum = 0;
			int i = 0; 

			while (i < s.Length -1)
			{
				if (rn[s[i]] < rn[s[i+1]]) {
					sum += rn[s[i+1]] - rn[s[i]];
					i += 2;
				}                                
				else {
					sum += rn[s[i]];
					i++;
				}    
			}

			if (i == s.Length -1)
				sum += rn[s[i]];

			return sum;         
		}
        
    }
```

<!-- public class Solution {
    public int RomanToInt(string s) {
        int convertedNumber = 0;
int counter = 0;
Dictionary<char, int> romanDictionary = new Dictionary<char, int>()
{
{ 'I', 1 },
{ 'V', 5 },
{ 'X', 10 },
{ 'L', 50 },
{ 'C', 100 },
{ 'D', 500 },
{ 'M', 1000 }
};

        while (counter < s.Length)
        {
            if (((counter + 1) < s.Length) &&
                (romanDictionary[s[counter]] > romanDictionary[s[counter+1]]))
            {
                convertedNumber += romanDictionary[s[counter]];
                counter++;
            }
            else if(((counter + 1) < s.Length) &&
                (romanDictionary[s[counter]] < romanDictionary[s[counter + 1]]))
            {
                convertedNumber += romanDictionary[s[counter + 1]] - romanDictionary[s[counter]];
                counter += 2;
            }
            else
            {
                convertedNumber += romanDictionary[s[counter]];
                counter++;
            }                
        }

        return convertedNumber;
    }
        
    }
 -->
```
  int convertedNumber = 0;
            int counter = 0;
            Dictionary<char, int> romanDictionary = new Dictionary<char, int>()
              {
              { 'I', 1 },
              { 'V', 5 },
              { 'X', 10 },
              { 'L', 50 },
              { 'C', 100 },
              { 'D', 500 },
              { 'M', 1000 }
              };

            while (counter < s.Length)
            {
                if (((counter + 1) < s.Length) &&
                    (romanDictionary[s[counter]] > romanDictionary[s[counter + 1]]))
                {
                    convertedNumber += romanDictionary[s[counter]];
                    counter++;
                }
                else if (((counter + 1) < s.Length) &&
                    (romanDictionary[s[counter]] < romanDictionary[s[counter + 1]]))
                {
                    convertedNumber += romanDictionary[s[counter + 1]] - romanDictionary[s[counter]];
                    counter += 2;
                }
                else
                {
                    convertedNumber += romanDictionary[s[counter]];
                    counter++;
                }
            }

            return convertedNumber;
```
