Skip to content

Commit 0ba9cdc

Browse files
author
Li Li
committed
add code of 13
1 parent 619c48c commit 0ba9cdc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int RomanToInt(string s) {
3+
if (s == null || s.Length == 0) return 0;
4+
Dictionary<char, int> dict = new Dictionary<char, int>() {
5+
{'I', 1},
6+
{'V', 5},
7+
{'X', 10},
8+
{'L', 50},
9+
{'C', 100},
10+
{'D', 500},
11+
{'M', 1000},
12+
};
13+
int sum = dict[s[s.Length - 1]];
14+
for (int i = s.Length - 2; i >= 0; i--) {
15+
if (dict[s[i]] < dict[s[i + 1]]) {
16+
sum -= dict[s[i]];
17+
} else {
18+
sum += dict[s[i]];
19+
}
20+
}
21+
return sum;
22+
}
23+
}

0 commit comments

Comments
 (0)