-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem0013RomanToInteger.cs
48 lines (42 loc) · 1.05 KB
/
Problem0013RomanToInteger.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
42
43
44
45
46
47
48
using CSharpLeetCode.Solutions.Library.Interfaces;
namespace CSharpLeetCode.Solutions.Library.Implementations;
public class Problem0013RomanToInteger : IProblem0013RomanToInteger
{
public int RomanToInt(string s)
{
var romanInt = RomanToIntArray(s);
var sum = 0;
for (int i = 0; i < romanInt.Length - 1; i++)
{
if (romanInt[i] >= romanInt[i + 1])
{
sum += romanInt[i];
}
else
{
sum -= romanInt[i];
}
}
sum += romanInt[^1];
return sum;
}
int[] RomanToIntArray(string s)
{
var romanInt = new int[s.Length];
for (int i = 0; i < s.Length; i++)
{
romanInt[i] = s[i] switch
{
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000,
_ => 0,
};
}
return romanInt;
}
}