[LeetCode] 13. Roman to Integer

Roman numerals are represented by seven different symbols: IVXLCD 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 ones 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.

Example 1:

Input: s = "III"
Output: 3
Explanation: III = 3.

Example 2:

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

Example 3:

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

Constraints:

  • 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].

罗马数字转换整数。

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

例如, 罗马数字 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。
给定一个罗马数字,将其转换成整数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/roman-to-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意跟12题恰好相反。这个题算是一道实现类的题目,思路是需要创建一个整数和罗马数字之间的 mapping,然后从右往左遍历 input 字符串。

这道题不难,但是罗马数字的规则需要花点时间理解清楚。根据规则,如果一个较小的字母出现在一个较大的字母左边则是做减法,如果一个较小的字母出现在一个较大的字母右边则是做加法。或者,对于某一个罗马数字 A 而言,如果他的左边出现了一个比他自己小的数字 B,则结果是 A - B,如果 B 出现在 A 的右边,则结果是 A + B。你知道记住这个规则这个题就瞬间变得很容易了。我参考了这个帖子

时间O(n)

空间O(n) - hashmap

JavaScript实现

 1 /**
 2  * @param {string} s
 3  * @return {number}
 4  */
 5 var romanToInt = function (s) {
 6     const map = new Map([
 7         ['I', 1],
 8         ['V', 5],
 9         ['X', 10],
10         ['L', 50],
11         ['C', 100],
12         ['D', 500],
13         ['M', 1000],
14     ]);
15     
16     let sum = 0;
17     let first = s.charAt(0);
18     let preNum = map.get(first);
19     for (let i = 1; i < s.length; i++) {
20         let num = map.get(s.charAt(i));
21         if (preNum < num) {
22             sum -= preNum;
23         } else {
24             sum += preNum;
25         }
26         preNum = num;
27     }
28     sum += preNum;
29     return sum;
30 };

 

Java实现

 1 class Solution {
 2     public int romanToInt(String s) {
 3         HashMap<Character, Integer> map = new HashMap<>();
 4         map.put('I', 1);
 5         map.put('V', 5);
 6         map.put('X', 10);
 7         map.put('L', 50);
 8         map.put('C', 100);
 9         map.put('D', 500);
10         map.put('M', 1000);
11 
12         int sum = 0;
13         char first = s.charAt(0);
14         int preNum = map.get(first);
15         for (int i = 1; i < s.length(); i++) {
16             int num = map.get(s.charAt(i));
17             if (preNum < num) {
18                 sum -= preNum;
19             } else {
20                 sum += preNum;
21             }
22             preNum = num;
23         }
24         sum += preNum;
25         return sum;
26     }
27 }

 

LeetCode 题目总结

posted @ 2020-01-29 03:08  CNoodle  阅读(480)  评论(0编辑  收藏  举报