Topic:
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Answer:
To be honest, the problem is bullying Chinese people don't know Roman numerals. Refer to the relevant Wikipedia: Portal. Among them are the following properties:
There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500), and M (1000).Right plus left minus: a smaller number of Roman numerals on the right side of the larger Roman numerals, indicating large numbers plus small numbers. The smaller Roman numerals on the left side of the larger Roman numerals indicate that the large numbers decrease the number of words.
The next thing is to check the table, and do not consider the problem of the system, a count of one.
Compares the size of the letter to the right, and if it is greater than or equal to the right letter, the sum is added to the letter;
Class Solution {public: int Romantoint (string s) { Std::map<char, int>table; table[' I '] = 1; table[' V '] = 5; table[' X '] = ten; table[' L '] =; table[' C '] = +; table[' D '] =; table[' M '] = +; int len = S.size (); int ans = 0; for (int i = 0; i<len-1; i++) { if (Table[s[i]] < table[s[i+1]]) ans = ans-table[s[i]]; else ans = ans + table[s[i]]; } Ans + = table[s[len-1]]; return ans; };
"Leetcode from zero single brush" Roman to Integer