-
Notifications
You must be signed in to change notification settings - Fork 30
/
RomantoInteger.cs
executable file
·42 lines (38 loc) · 2.2 KB
/
RomantoInteger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Source : https://leetcode.com/problems/roman-to-integer/
// Author : codeyu
// Date : 10/5/16
/***************************************************************************************
*
* Given a roman numeral, convert it to an integer.
* Input is guaranteed to be within the range from 1 to 3999.
*
**********************************************************************************/
using System.Collections.Generic;
namespace Algorithms
{
public class Solution013
{
public static int RomanToInt(string s)
{
Dictionary<char,int> dict = new Dictionary<char,int>{
{'M',1000},
{'D',500},
{'C',100},
{'L',50},
{'X',10},
{'V',5},
{'I',1}
};
int i = s.Length-1;
int ret = dict[s[i--]];
while(i>=0)
{
if(dict[s[i+1]] > dict[s[i]])
ret -= dict[s[i--]];
else
ret += dict[s[i--]];
}
return ret;
}
}
}