-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternative.cs
More file actions
41 lines (40 loc) · 903 Bytes
/
Alternative.cs
File metadata and controls
41 lines (40 loc) · 903 Bytes
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
namespace LeetCode.Problem13{
//13. Roman to Integer
//https://leetcode.com/problems/roman-to-integer/
/*
Given a roman numeral, convert it to an integer.
*/
public class Alternative {
public int RomanToInt(string s) {
int answer = 0;
int num = 0;
for(int i = s.Length-1; i >= 0; i--){
switch(s[i]){
case 'I': num = 1;
break;
case 'V': num = 5;
break;
case 'X': num = 10;
break;
case 'L': num = 50;
break;
case 'C': num = 100;
break;
case 'D': num = 500;
break;
case 'M': num = 1000;
break;
}
if(4 * num < answer)
{
answer -= num;
}
else
{
answer += num;
}
}
return answer;
}
}
}