1. Title:
Original title: Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Subscribe to see which companies asked this questio
Parse: Give a Roman number and ask to convert it to an integer. The input range is within 1 to 3999.
The rules of Roman numerals are as follows:
Roman numerals |
I |
V |
X |
L |
C |
D |
M |
Arabic numerals on behalf of |
1 |
5 |
10 |
50 |
100 |
500 |
1000 |
Among them are:
- The basic number Ⅰ, X, C any one, the number of their own use, or put on the right side of the large number of the composition, not more than three; on the left of the large number can only be used one;
- It is not possible to use any one of the basic digits V, L, D as decimals on the left side of the large number to make up the number by subtracting the method;
- I can only be used on V and X left;
- X can only be used on the left of L and C;
- C can only be used on the left of D and M.
Example:
• Single digit examples ⅰ-1, ⅱ-2, ⅲ-3, ⅳ-4, ⅴ-5, ⅵ-6, ⅶ-7, ⅷ-8, ⅸ-9 10-digit examples ⅹ-10, ⅺ-11, ⅻ-12, XIII-13, XIV-14, XV-15, XVI-16, XVII-17, XVIII-18, XIX-19, XX-20, XXI-21, XXII-22, XXIX-29, XXX-30, XXXIV-34, XXXV-35, XXXIX-39, XL-40, L-50, LI-51, LV-55, LX-60, LXV-65, LXXX-80, XC-90, XCIII-93, XCV-95, XCVIII-98, xcix-99 hundred examples C-100, CC-200, CCC-300, CD-400, D-500, DC-600, DCC-700, DCCC-800 , CM-900, cmxcix-999 Thousands of examples M-1000, MC-1100, MCD-1400, MD-1500, MDC-1600, MDCLXVI-1666, MDCCCLXXXVIII-1888, MDCCCXCIX-1899, MCM-1900, MCMLXXVI-1976, MCMLXXXIV-1984, MCMXC-1990, MM-2000, MMMCMXCIX-3999 reference: Roman numerals
2. Ideas
There are two kinds of ideas, one is the direct if-else mode (this mode is better than the second one), the second is switch, the second runtime performance is not good. This sparked my curiosity about if-else and switch, and fortunately someone has summed up this part of the content:
As a result, switch has a bit of space for time, and in fact it does.
1. when there are more branches, the efficiency of using switch is very high . Because switch is randomly accessed, it is determined that the selection value jumps directly to that particular branch, but if. else is the traversal so that it is possible to find the branch that meets the criteria. So, switch is actually much more efficient than ifelse.
2. By the above assembly code can be known, switch...case occupies a lot of code space, because it to generate a skip table, especially when the case constant distribution range is very large but the actual valid value is relatively small,switch...case space utilization will become very low .
3.switch...case can only handle cases where the case is constant, and is powerless in the case of a very limited number of cases. For example, if (a > 1 && a < 100), it is not possible to use switch...case for processing. Therefore, switch can only be used in the constant selection of branches more efficient than ifelse, but IfElse can be applied to more occasions,ifelse more flexible .
Reference: Efficiency issues with switch and IfElse
3. Two ways of Thinking code
The first type:
Class Solution {public: int Romantoint (string s) { int ans=0,m=s.size (); char ch; for (int i=0;i<m;i++) { if (s[i]== ' m ') ans+=1000; else if (s[i]== ' D ') ans+=500; else if (s[i]== ' C ') { if (s[i+1]== ' D ' | | | s[i+1]== ' M ') &&i+1<m) ans-=100; else ans+=100; } else if (s[i]== ' X ') { if (s[i+1]== ' L ' | | | s[i+1]== ' C ') &&i+1<m) ans-=10; else ans+=10; } else if (s[i]== ' V ') ans+=5; else if (s[i]== ' I ') { if (s[i+1]== ' V ' | | s[i+1]== ' X ') &&i+1<m) ans--; else ans++; } else if (s[i]== ' L ') ans+=50; } return ans; };
The second type:
Class Solution {public: int value (CHAR-ch) { switch (CH) {case ' I ': return 1; Case ' V ': return 5; Case ' X ': return ten; Case ' L ': return; Case ' C ': return; Case ' D ': return; Case ' M ': return; } return 0;} int Romantoint (string s) { int ans = 0; char max = ' I '; for (int i = S.size ()-1; I >= 0; i) { if (value (S[i]) >= value (max)) { max = s[i]; Ans + = value (S[i]); } else { ans-= value (S[i]);} } return ans;} ;
LeetCode-13. Roman to Integer-think of the comparison between If-else and switch-(c + +)-Problem Solving report