I. Title Description
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Two. Topic analysis
Let's summarize the Roman numerals, which are some of the explanations found online:
Roman numerals are the oldest representation of numbers, more than 2000 years earlier than Arab arrays, originated in Rome ...
Roman numerals have the following symbols:
Basic characters: I V X L C D M
Corresponding Arabic numerals: 1 5 10 50 100 500 1000
Count rule: The same number ligatures, the number represented is equal to the number added to the number, for example: the III = 3 small number on the right side of the large number, the number represented is equal to the number of these numbers added, for example: VIII = 8 small numbers, Limited (I, X and C) on the left of large numbers, The number represented is equal to the number of the large number minus the decimal, for example: IV = 4 in normal use, consecutive digits cannot be repeated more than three times on a number of lines, indicating that the number is enlarged 1000 .
The idea of the topic , starting with the first character of the input string (the Roman numeral is traversed backwards), assuming that there is a temporary variable, the k result is stored in the result variable, the algorithm execution process is divided into the following three situations:
If the current character has a smaller number than the previous character, you can first add the value of the temporary variable k to the result result , and then start the next record. For example VI = 5 + 1 , at this point ‘I‘ the current is, less than the previous bit, and the ‘V‘ k = 5 current bit corresponds to the value, then the curr = 1 result is updated to: result = k + curr , and then update the temporary variable k = curr .
If the current character corresponds to the same number as the previous character, then the temporary variable is k added to the character. For example XXX = 30 , in this process k = 10 + 10 + 10 .
If the current character corresponds to a number larger than the previous character, it means that the value of the paragraph should be the current value minus the value in the temporary variable that was previously accumulated k , for example IIV = 5 – 2 .
Three. Sample code
Class solution{ Public:int Getromanvalue(Charc) {Switch(c) { Case ' I ':return 1; Case ' V ':return 5; Case ' X ':return Ten; Case ' L ':return -; Case ' C ':return -; Case ' D ':return -; Case ' M ':return +;default:return 0; } }intRomantoint (strings) {if(S.size () <1)return-1;intresult =0;inttemp = Getromanvalue (s[0]);intK = temp; for(size_t i =1; I < s.size (); i++) {intCurr = Getromanvalue (S[i]);if(Temp > Curr) {result + = k; K = Curr; }Else if(temp = = Curr) k = k + Curr;ElseK = curr-k; temp = Curr; } result + = k;returnResult }};
Four. Summary
There are more simple ideas on the Web:
classSolution { Public:intRomantoint (strings) {int Map[ -];Map[' I '-' A '] =1;Map[' V '-' A '] =5;Map[' X '-' A '] =Ten;Map[' L '-' A '] = -;Map[' C '-' A '] = -;Map[' D '-' A '] = -;Map[' M '-' A '] = +;intres =0, n = s.size (); S.push_back (S[n-1]); for(inti =0; I < n; i++) {if(Map[S[i]-' A '] >=Map[S[i +1] -' A ']) Res + =Map[S[i]-' A '];ElseRes-=Map[S[i]-' A ']; }returnRes }};
Corresponding title: Integer to Roman.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode Note: Roman to Integer