LeetCode_13 Roman numerals to Integer

Title Description

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

character Numerical
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
罗马数字 2 写做 II ,即为两个并列的 1。
12 写做 XII ,即为 X + II 。
27 写做  XXVII, 即为 XX + V + II 。

Typically, small numbers of Roman numerals to the right in large numbers. But there are exceptions, special rules apply 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.

Examples

Input: "III"
Output: 3

Input: "IV"
Output: 4

Enter: "IX"
Output: 9

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

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

Knowledge Point

  • Roman numerals
  • dictionary
  • Hash table
  • String String

Hash tables / switch

Data is stored by a key (key) and the value (value) thereof.

string.at(i)

Can be obtained in the i-th bit string

string.size(string)

String size can be obtained

solution

Ideas left a small cut, left big plus

规则:
罗马数字由 I,V,X,L,C,D,M 构成;
当小值在大值的左边,则减小值,如 IV=5-1=4;
当小值在大值的右边,则加小值,如 VI=5+1=6;
由上可知,右值永远为正,因此最后一位必然为正。
一言蔽之,把一个小值放在大值的左边,就是做减法,否则为加法。

Algorithmic process

  • 1. Assuming that the input is valid Roman word
  • 2. The transfer function: Roman numerals I, V, X, L, C, D, M corresponding digital
  • 3. If the second Roman numerals <first Roman numeral (call transfer function), the sum
  • 4. If the second Roman numerals> first Roman numeral (call transfer function), the subtraction
  • 5. returns the result obtained

Code

//C++
#include <iostream>
#include<string>
using namespace std;
class Solution {
public:
    int romanToInt(string s) {//输入的是字符串
        int sum = 0;//sum记录当前右值
        int prenum = getValue(s.at(0));//字符串的第一个字符转换成数字,用prenum记录左值
        for (int i = 1; i < s.size(); ++i) {//string size()返回字符串大小,每次循环处理prenum
            int postnum = getValue(s.at(i));//用postnum记录右值
            if (postnum > prenum) //如果小值在左边prenum,sum-左值
                sum -= prenum;
          
            else//如果小值在右边,sum+左值
                sum += prenum;
            prenum = postnum;//比较完相邻两个字符的大小后,把sum赋值给prenum,作为下一次比较的左值
        }
        sum += prenum;//最后一次跳出循环的时候,把最后一个postnum赋值给了prenum,所以最后sum还要加上prenum
        return sum;

    }

//函数需要设置为public???
    int getValue(char ch) {//对每一个字符进行转换成数字
        switch (ch) {
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
        default: return 0;
        }
    }

};

int main() {
    Solution solution;
    string c1 = "IXX";
    string c2 = "IV";
    string c3 = "LVIII";
    string c4 = "MCMXCIV";
    cout << solution.romanToInt(c1) << endl;
    cout << solution.romanToInt(c2) << endl;
    cout << solution.romanToInt(c3) << endl;
    cout << solution.romanToInt(c4) << endl;
}


Guess you like

Origin www.cnblogs.com/zhuomoyixia/p/12403608.html