Leetcode 13. Roman number to integer C language concise 95.79%

【topic】

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

Character value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, Roman numeral 2 is written as II, which is two parallel 1. 12 is written as XII, which is X + II. 27 is written as XXVII, which is XX + V + II.

Normally, the small number in Roman numerals is to the right of the large number. But there are special cases, for example, 4 is not written as IIII, but IV. The number 1 is to the left of the number 5, and the number represented is equal to the number 4 obtained by reducing the number 1 by the number 5. Similarly, the number 9 is represented as IX. This special rule only applies to the following six situations:

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

Example 1:

Input: "III"
Output: 3
Example 2:

Input: "IV"
Output: 4
Example 3:

Input: "IX"
Output: 9
Example 4:

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

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90, IV = 4.

 

【Thinking】

The solution I came up with when I was taking a bath, I found it coincided with the JAVA boss

For this question, consider comparing the current number with the next number. If it is smaller than the next number, subtract it; if it is larger than the next number, add it.
The hash table takes up too much, the comparison of a few letters does not require that much space cost, just using switch is enough.
Because you only need to consider the two numbers before and after, the comparison can be discarded directly after the addition and subtraction operations are completed, so set two variables pre and cur, cur is the corresponding number of the Roman alphabet currently scanned in, pre is the previous number, initialized to 0 .
sum only adds and subtracts pre, just add the judgment. After the end of the for loop, the last number is not calculated, so add the last value of pre

 1 int romanToInt(char * s){
 2     int pre = 0, cur = 0;
 3     int sum = 0;
 4     for (int i = 0; i < strlen(s); i++) {
 5         switch(s[i]) {
 6             case 'I': cur = 1; break;
 7             case 'V': cur = 5; break;
 8             case 'X': cur = 10; break;
 9             case 'L': cur = 50; break;
10             case 'C': cur = 100; break;
11             case 'D': cur = 500; break;
12             case 'M': cur = 1000; break;
13         }
14         if (pre < cur) {
15             sum -= pre;
16         }
17         else {
18             sum += pre;
19         }
20         pre = cur;
21     }
22     sum += pre;
23     return sum;
24 }

When I first considered the algorithm, I thought that if I use an array to store it, it will not only occupy a large amount, but also need to be looped many times, which is too time-consuming. I wonder if I can calculate while traversing the analysis. Then I thought of the linked list. Can I throw it while counting it, and then think that the variable is not fragrant? ? ? There is no throwing operation, just assign the value to the new one!

 

 

 

Guess you like

Origin www.cnblogs.com/jhjgarden/p/12702560.html