Roman to Integer
I first used a dictionary to map each Roman symbol to its value, then I traverse the string one by one.
For each character, I get its value and try to build the total answer.
My main confusion was that I was only focusing on the current character. I didn’t realize I also need to compare it with the next character to decide whether to add or subtract.
Now I understand:
I first used a dictionary to map each Roman symbol to its value, then I traverse the string one by one.
For each character, I get its value and try to build the total answer.
My main confusion was that I was only focusing on the current character. I didn’t realize I also need to compare it with the next character to decide whether to add or subtract.
Now I understand:
If current value < next value → subtract
Otherwise → add