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 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 beforeV (5)
andX (10)
to make4
and9
.X
can be placed beforeL (50)
andC (100)
to make40
and90
.C
can be placed beforeD (500)
andM (1000)
to make400
and900
.
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]
.
Thought:
- 定义一个哈希表,将罗马数字的每个字符映射到对应的整数值上。
- 初始化一个变量
ans
为0
,表示最终的整数值
。 - 遍历输入的罗马数字字符串
s
,对于每个字符
:
a. 获取其对应的整数值value
。
b. 如果当前字符不是字符串的最后一个字符,且其对应的整数值小于其后面字符对应的整数值,则将 ans 减去value
。
c. 否则将ans
加上value
。 - 遍历完成后,返回
ans
作为结果。
AC:
/*
* @lc app=leetcode.cn id=13 lang=cpp
*
* [13] 罗马数字转整数
*/
// @lc code=start
class Solution {
public:
unordered_map<char, int> symbolValue = {
{
'I', 1},
{
'V', 5},
{
'X', 10},
{
'L', 50},
{
'C', 100},
{
'D', 500},
{
'M', 1000}
};
int romanToInt(string s) {
int ans = 0;
int len = s.length();
for(int i = 0; i < len; i++)
{
int value = symbolValue[s[i]];
if(i < len - 1 && value < symbolValue[s[i + 1]])
{
ans -= value;
}
else
ans += value;
}
return ans;
}
};
// @lc code=end
Supplement:
C++
标准库提供了unordered_map
容器,用于存储键-值对
。下面介绍unordered_map
的使用方法。
- 头文件
#include <unordered_map>
- 定义
unordered_map
std::unordered_map<std::string, int> my_map;
上面定义了一个unordered_map,键为string类型,值为int类型。
- 插入元素
my_map.insert({"apple", 3});
my_map["banana"] = 5;
支持使用insert
函数和[]
运算符来插入元素。
- 访问元素
std::cout << my_map["apple"] << std::endl;
可以通过[]
运算符访问元素,如果该键不存在,则会自动插入一个默认值。
- 遍历元素
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
使用迭代器遍历元素,其中it->first
表示键,it->second
表示值。
- 删除元素
my_map.erase("apple");
使用erase
函数删除元素。
- 判断元素是否存在
if (my_map.find("apple") != my_map.end()) {
std::cout << "The element is found." << std::endl;
}
使用find
函数判断元素是否存在,如果该键存在,则返回该键对应的迭代器,否则返回end()
迭代器。
unordered_map
的基本操作
std::unordered_map<std::string, int> my_map;
my_map.emplace("apple", 3);
my_map.insert({
"banana", 5});
my_map["apple"] = 2;
std::cout << my_map["apple"] << std::endl;
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
my_map.erase("apple");
if (my_map.find("apple") != my_map.end()) {
std::cout << "The element is found." << std::endl;
} else {
std::cout << "The element is not found." << std::endl;
}
以上就是unordered_map
的基本操作方法。