leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0013.cpp (690B)
0 class Solution {
1 public:
2 int value(char c) {
3 switch (c) {
4 case 'I': return 1;
5 case 'V': return 5;
6 case 'X': return 10;
7 case 'L': return 50;
8 case 'C': return 100;
9 case 'D': return 500;
10 case 'M': return 1000;
11 default: return -10000;
12 }
13 }
15 int romanToInt(string s) {
16 int size = s.size();
17 int res = 0;
18 for (int i = 0; i < size - 1; i++) {
19 int a = value(s[i]);
20 int b = value(s[i + 1]);
21 if (a >= b)
22 res += a;
23 else
24 res -= a;
25 }
26 res += value(s[size - 1]);
28 return res;
29 }
30 };