-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0013-RomanToInteger.cs
188 lines (183 loc) · 4.98 KB
/
0013-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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using Xunit;
using Util;
using System.Text;
namespace RomanToInteger
{
public class Solution
{
// Symbol Value
// I 1
// V 5
// X 10
// L 50
// C 100
// D 500
// M 1000
public int RomanToInt(string s)
{
int res = 0;
int idx = 0;
while (s[idx] == 'M')
{
res += 1000;
idx++;
if (idx >= s.Length)
return res;
}
s = s.Substring(idx);
if (s.StartsWith("CM"))
{
res += 900;
if (s.Length == 2)
return res;
s = s.Substring(2);
}
if (s.StartsWith("D"))
{
res += 500;
if (s.Length == 1)
return res;
s = s.Substring(1);
}
if (s.StartsWith("CD"))
{
res += 400;
if (s.Length == 2)
return res;
s = s.Substring(2);
}
// X 10
// L 50
// C 100
idx = 0;
while (s[idx] == 'C')
{
res += 100;
idx++;
if (idx >= s.Length)
return res;
}
s = s.Substring(idx);
if (s.StartsWith("XC"))
{
res += 90;
if (s.Length == 2)
return res;
s = s.Substring(2);
}
if (s.StartsWith("L"))
{
res += 50;
if (s.Length == 1)
return res;
s = s.Substring(1);
}
if (s.StartsWith("XL"))
{
res += 40;
if (s.Length == 2)
return res;
s = s.Substring(2);
}
// I 1
// V 5
// X 10
idx = 0;
while (s[idx] == 'X')
{
res += 10;
idx++;
if (idx >= s.Length)
return res;
}
s = s.Substring(idx);
if (s.StartsWith("IX"))
{
res += 9;
if (s.Length == 2)
return res;
s = s.Substring(2);
}
if (s.StartsWith("V"))
{
res += 5;
if (s.Length == 1)
return res;
s = s.Substring(1);
}
if (s.StartsWith("IV"))
{
res += 4;
if (s.Length == 2)
return res;
s = s.Substring(2);
}
idx = 0;
while (s[idx] == 'I')
{
res += 1;
idx++;
if (idx >= s.Length)
return res;
}
return res;
}
}
public class Test
{
static public void Run()
{
Console.WriteLine("RomanToInteger");
// Console.WriteLine(new Solution().RomanToInt("III"));
// Console.WriteLine(new Solution().RomanToInt("MM"));
// Console.WriteLine(new Solution().RomanToInt("MCMXCIV"));
// Console.WriteLine(new Solution().RomanToInt("MMMXL"));
// return;
string input;
int exp, res;
using (new Timeit())
{
input = "III";
exp = 3;
res = new Solution().RomanToInt(input);
Assert.Equal(exp, res);
}
using (new Timeit())
{
input = "IV";
exp = 4;
res = new Solution().RomanToInt(input);
Assert.Equal(exp, res);
}
using (new Timeit())
{
input = "IX";
exp = 9;
res = new Solution().RomanToInt(input);
Assert.Equal(exp, res);
}
using (new Timeit())
{
input = "LVIII";
exp = 58;
res = new Solution().RomanToInt(input);
Assert.Equal(exp, res);
}
using (new Timeit())
{
input = "MCMXCIV";
exp = 1994;
res = new Solution().RomanToInt(input);
Assert.Equal(exp, res);
}
using (new Timeit())
{
input = "MMXIX";
exp = 2019;
res = new Solution().RomanToInt(input);
Assert.Equal(exp, res);
}
}
}
}