I like to read this solution as a small machine: keep the useful information, throw away the noise. For 13. Roman to Integer, the solution in this repository is mainly a two pointers solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are look2, look1, romanToInt.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
03
04Symbol Value
05I 1
06V 5
07X 10
08L 50
09C 100
10D 500
11M 1000
12For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
13
14Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
15
16I can be placed before V (5) and X (10) to make 4 and 9.
17X can be placed before L (50) and C (100) to make 40 and 90.
18C can be placed before D (500) and M (1000) to make 400 and 900.
19Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
20
21Example 1:
22
23Input: "III"
24Output: 3
25Example 2:
26
27Input: "IV"
28Output: 4
29Example 3:
30
31Input: "IX"
32Output: 9
33Example 4:
34
35Input: "LVIII"
36Output: 58
37Explanation: L = 50, V= 5, III = 3.
38Example 5:
39
40Input: "MCMXCIV"
41Output: 1994
42Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
43**/
44
45//Runtime: 80 ms, faster than 8.14% of C++ online submissions for Roman to Integer.
46//Memory Usage: 36.6 MB, less than 5.10% of C++ online submissions for Roman to Integer.
47class Solution {
48public:
49 map<string, int> twoMap;
50 map<char, int> oneMap;
51
52 bool look2(string s, int& ans, int& i){
53 if(twoMap.find(s)!=twoMap.end()){
54 ans += twoMap[s];
55 i += 2;
56 return true;
57 }
58 return false;
59 }
60
61 bool look1(char c, int& ans, int& i){
62 if(oneMap.find(c)!=oneMap.end()){
63 ans += oneMap[c];
64 i += 1;
65 return true;
66 }
67 return false;
68 }
69
70 int romanToInt(string s) {
71 int ans = 0;
72 int i = 0;
73
74 twoMap["IV"] = 4;
75 twoMap["IX"] = 9;
76 twoMap["XL"] = 40;
77 twoMap["XC"] = 90;
78 twoMap["CD"] = 400;
79 twoMap["CM"] = 900;
80
81 oneMap['I'] = 1;
82 oneMap['V'] = 5;
83 oneMap['X'] = 10;
84 oneMap['L'] = 50;
85 oneMap['C'] = 100;
86 oneMap['D'] = 500;
87 oneMap['M'] = 1000;
88
89 while(i < s.size()){
90 if(i+1 < s.size()){
91 string subs = s.substr(i, 2);
92 bool b = look2(subs, ans, i);
93 if(!b){
94 look1(s[i], ans, i);
95 }
96 }else{
97 look1(s[i], ans, i);
98 }
99 }
100 return ans;
101 }
102};
Cost