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 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')
.s
is a valid roman numeral in the range [1, 3999]
.Thought:
ans
为 0
,表示最终的整数值
。s
,对于每个字符
:value
。value
。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
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
的基本操作方法。