Leetcode13_ Roman numerals to Integer

topic

Roman numeral characters comprising the following seven: I, V, X, L, C, D and M.

Numerical character
the I. 1
V. 5
X-10
L 50
C 100
D 500
M 1000
, for example, 2 written as Roman numerals II, namely 1.12 written as two parallel XII, namely X + II. 27 written as XXVII, namely XX + V + II.

Typically, small numbers of Roman numerals to the right in large numbers. But there are exceptions, for example, do not write 4 IIII, but IV. In the left number 5 number 1, the number of large numbers equal to the value represented by a reduced number 5 obtained 4. Likewise, the number 9 is represented as IX. This special rule applies only to the following six cases:

I may be on the left V (5) and X (10), and to represent 4 and 9.
X L can be placed on the left (50) and C (100), and 40 and 90 are represented. 
C may be placed on D (500) and M (1000) to the left, to 400 and 900 represent.
Given a Roman numeral, to convert it to an integer. To ensure that the input is in the range of 1 to 3999.

Example 1:

Input: "III"
Output: 3


Example 2:

Input: "IV"
Output: 4


Example 3:

Enter: "IX"
Output: 9


Example 4:

Input: "LVIII"
Output: 58
explains: L = 50, V = 5 , III = 3.


Example 5:

Enter: "MCMXCIV"
Output: 1994


Explanation: M = 1000, CM = 900, XC = 90, IV = 4.

 

Thinking

That is to say that six cases, only the current value is less than those of the latter, only subtraction of the former, it is just a time and a contrast of characters on the line, other cases are additions

 

Code

. 1  class Solution {
 2  public :
 . 3      int romanToInt ( String S) {
 . 4          int Chang = s.size ();
 . 5          int SUM = 0 , index = 0 ;
 . 6          for (index = 0 ;; index ++ ) {
 . 7              IF (index chang- == . 1 ) {
 . 8                  SUM + = GetSum (S [index]); // index careful not written chang, chang because of overflow
 . 9                  BREAK ;
 10              }    
 . 11              the else {
 12 is                 if(getsum(s[index])<getsum(s[index+1]))
13                     sum-=getsum(s[index]);
14                 else
15                     sum+=getsum(s[index]);
16             }
17                 
18         }   
19     
20     return sum;
21 }
22         int getsum(char c){
23             int num;
24             switch(c){
25                 case 'I':num=1;
26                     break;
27                 case 'V':num=5;
28                     break;
29                 case 'X':num=10;
30                     break;
31                 case 'L':num=50;
32                     break;
33                 case 'C':num=100;
34                     break;
35                 case 'D':num=500;
36                     break;
37                 case 'M':num=1000;
38                     break;
39                     
40             }
41             return num;
42         }
43         
44     
45 };

 

Interpreted code

22-42 line with a function to assign to each Roman numeral, followed by the value of this function to call each letter, and finally return of a call that is num.

 

chang 3-21 trip is to determine the length of the input Roman characters, sum is the total value, index is now subscript character position

7-8 when the index line is the last position, nor the value on the right, the left and right without comparison value of the size, so coupled directly to s [index]

11 is the case when the line if not the last character

12-13 line first determines whether the value is less than the value of the right to the left, less than then it subtracts the value of the left

14-15 rows to the left of the value is greater than the right value, then it would add value to the left

return sum 20 returns the last row is the sum of the braces 21 at this time is that the first row line 3, because the next function is the custom, the first row belonging to class solution.

 

reward

s.size, when s is a string type, length is acquired

Using custom functions

Syntax need to wrap switch case statement, because this deficiency found themselves, such as single quotes after the case, and the case colon

This question is of the highest praise is divided python dictionary written, it really is python Come on, but all roads lead to Rome, choose to go their own choice like

 

This question is from 6:00 night began to write, go to the middle of the playground running, exercise, until 22 o'clock more than just write. But after exercise themselves back to see the spirit of the code is suddenly very focused, and today sports, when they like traveling back in time to play the kind of freshman boxing indomitable and optimistic attitude, this time is always mourning the victims retire after exercise mentality, so I became the victims and, I do not bother, is not afraid to write code detours, detours is the biggest fear detours, there are two words, not Tang donate a work called, called the same thing. With the trouble to solve problems, difficult to solve difficult method than problems, to life in a kind of an optimistic and positive attitude towards life, you need to take life seriously, I love life. Write code but also become the focus of my attention and serious thought process, I am enjoying the code from the ignorant, so I searched through all kinds of information through various channels, get to know the feeling of it, I like this feel.

 

Originally this blog is 22 points more than going to write, but the middle of a new understanding of the students we discuss a data visualization of the game, I'm looking for data for the period, so it is delayed until now, I have delayed about a 3, 4 leetcode title did not write, continue to fuel tomorrow!

 

Original title link

13. Roman numerals to Integer

 

Guess you like

Origin www.cnblogs.com/vocoub/p/11570205.html