Leetcode No.13 Roman to Integer罗马数字转整数(c++实现)

1. 题目

1.1 英文题目

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.

1.2 中文题目

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。

字符 数值
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 。

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。

1.3输入输出

输入 输出
s = "III" 3
s = "IV" 4
s = "IX" 9
s = "LVIII" 58
s = "MCMXCIV" 1994

1.4 约束条件

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

2. 分析

2.1 特别处理法

如果不涉及4,9之类的特殊数字,其他部分只需要累加即可,因此可以对一般数字直接累加,对于特殊数字进行特殊处理。代码如下:

class Solution {
public:
    int romanToInt(string s) {
        int sum = 0;
        for (unsigned i = 0; i < s.size(); i++)
        {
            switch (s[i])//普通情况,直接累加
            {
            case 'I': sum += 1; break;
            case 'V': sum += 5; break;
            case 'X': sum += 10; break;
            case 'L': sum += 50; break;
            case 'C': sum += 100; break;
            case 'D': sum += 500; break;
            case 'M': sum += 1000; break;
            }

            if (i != 0)//特殊情况处理
            {
                if ((s[i - 1] == 'C') && (s[i] == 'D' || s[i] == 'M')) sum -= 200;//400,900情况
                else if ((s[i - 1] == 'X') && (s[i] == 'L' || s[i] == 'C')) sum -= 20;//40,90情况
                else if ((s[i - 1] == 'I') && (s[i] == 'V' || s[i] == 'X')) sum -= 2;//4,9情况
            }
        }
        return sum;
    }
};

参考:https://www.cnblogs.com/tianjiale/p/10120702.html

2.2 哈希表+减法

一般情况下,左边数字大于右边,也就可以左边加右边即为结果;特殊情况(4,9等),左边数字小于右边,右边减去左边数字即为结果。代码如下:

class Solution {
public:
    int romanToInt(string s) {
        unordered_map<char, int> hash_map = {
            { 'I', 1 },
            { 'V', 5 },
            { 'X', 10 },
            { 'L', 50 },
            { 'C', 100 },
            { 'D', 500 },
            { 'M', 1000 }
        };
        int sum = 0;
        for (unsigned int i = 0; i < s.size(); i++)
        {
            if (i != s.size() - 1 && hash_map[s[i]] < hash_map[s[i + 1]])
                sum -= hash_map[s[i]];
            else
                sum += hash_map[s[i]];
        }
        return sum;
    }
};

参考自:https://blog.csdn.net/qq_41562704/article/details/85446485

上一篇:matplotlib设置宋体和Times New Roman体


下一篇:领军人才企业投身到嘉兴智慧城市建设 助推创业创新