13. Roman to Integer

本文介绍了一种将罗马数字转换为整数的有效算法。通过分析罗马数字的构成规则,利用哈希表映射罗马字符到其对应的数值,并通过遍历字符串判断相邻字符的大小关系来实现正确的转换。

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.


Seen this question in a real interview before?    
Yes
 


翻译:给定一串罗马数字,将罗马数字转化成整数

分析:
罗马数字有 I 代表1    V代表5   X代表10    L代表50  C代表100    D代表500 M 代表1000
并且还要注意罗马数字的顺序1-10   I    II    III   IV   V  VI    VII VIII  IX  X
可以看出4和6的区别,当代表大数的字母在后时,比大数代表的数字少1.例如 XIX 这个是19  XX 代表20   XXI 代表21
所以,根据字母前后顺序判断数字的大小。大数在前,小数在后,两者相加      小数在前,大数在后,两者相减
也就是说,我们在遍历字符串的时候,如果当前字符代表数字小于前面的代表数字,则减去当前数字 如果不小于,则加上当前字母代表的大小

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



### Convert an Integer to a Roman Numeral with Subtractive Rules To convert an integer to a Roman numeral, especially accounting for subtractive combinations like IV (4), IX (9), XL (40), and so on, you can follow a structured approach using predefined mappings of integers to Roman numerals. This method ensures that the subtractive notation is applied correctly. The core idea is to use two arrays: one for the integer values and another for their corresponding Roman numeral representations. These arrays are ordered from the largest to the smallest values, including the special subtractive cases. The algorithm iterates through these values, subtracting them from the input number while appending the corresponding Roman numeral symbols to the result string. Here’s how this can be implemented in Python: ```python def int_to_roman(num): # Define the mapping of integers to Roman numerals, including subtractive cases val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] syms = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] roman_numeral = "" i = 0 while num > 0: while num >= val[i]: roman_numeral += syms[i] num -= val[i] i += 1 return roman_numeral ``` This function works by repeatedly subtracting the largest possible value from the given number and appending the corresponding Roman numeral symbol(s) to the result string. For example, when converting the number 1994, the function first subtracts 1000 (M), then 900 (CM), followed by 90 (XC), and finally 4 (IV), resulting in the Roman numeral "MCMXCIV" [^2]. ### Explanation of Subtractive Notation Handling Subtractive notation is used in specific cases where a smaller numeral precedes a larger one, indicating subtraction rather than addition. These cases include: - IV for 4 (5 - 1) - IX for 9 (10 - 1) - XL for 40 (50 - 10) - XC for 90 (100 - 10) - CD for 400 (500 - 100) - CM for 900 (1000 - 100) By including these subtractive combinations in the `val` and `syms` arrays, the function ensures that the correct Roman numeral representation is generated without requiring additional logic to handle these cases separately [^3]. ### Example Usage For instance, if you call `int_to_roman(1994)`, the function will return `"MCMXCIV"`, which accurately represents the year 1994 in Roman numerals [^2]. Similarly, calling `int_to_roman(58)` will return `"LVIII"`, representing 58 as L (50), V (5), and III (3).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值