Topic
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Analysis
This topic is a variant of the previous question, which requires the conversion of a given Roman sequence number to the corresponding integer.
Learn about the correspondence between Roman numerals and integers:
The corresponding examples are as follows:
AC Code
Class solution{ Public:Solution() {} ~solution () {}intValue (Charc) {Switch(c) { Case ' I ':return 1; Break; Case ' V ':return 5; Break; Case ' X ':return Ten; Break; Case ' L ':return -; Break; Case ' C ':return -; Break; Case ' D ':return -; Break; Case ' M ':return +; Break;default:return 0; Break; } }intRomantoint (strings) {intRET =0;if(s = ="")returnRet RET + = Value (s[0]); for(inti =0; s[i+1] !=' + '; i++) {if(Value (S[i +1]) <= value (S[i]) ret + = Value (S[i +1]);ElseRET = ret + Value (s[i +1]) -2* Value (s[i]); }returnRet }};
Git test code
Leetcode (Roman) to Integer