*************
C++
topic: 13. 罗马数字转整数 - 力扣(LeetCode)
*************
This topic is defined as easy and I donot understand.
![]() |
I have no idea to solve the problem. I would complain about the complex about the roman numbers designing. It is not simple and concise. I always pursuit of achieveing goals with simple things. Maximising the use of resources is my goal. When talks about elegent, it will be simple, comcise and logical. I give you an example.
|
OK back now.
In the topic, the first thing is to tell the program what is the number that each letter represents.
Introduce Hsah Map. Hash Map stores data, and can find, insert and delete data.
操作 | 代码示例 | 注意事项 |
---|---|---|
插入元素 | map[key] = value 或 insert({key, value}) | 直接赋值更简洁,insert 可避免覆盖已有值 |
查找元素是否存在 | if (map.find(key) != map.end()) | 不要直接用map[key] 检查,会创建空值! |
删除元素 | map.erase(key) | 直接通过键删除 |
遍历 | for (const auto& pair : map) | pair.first 是键,pair.second 是值 |
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
// 1. 创建一个哈希表:键是int,值是string
unordered_map<int, string> numberMap;
// 2. 插入数据:数字1-5对应的英文单词
numberMap[1] = "One";
numberMap.insert({2, "Two"}); // 插入元素的第二种方法
numberMap[3] = "Three"; // 还是使用直接赋值,这个看起来更简洁
numberMap[4] = "Four";
numberMap[5] = "Five";
// 3. 访问元素:查找键为3的值
std::cout << "3对应的单词是: " << numberMap[3] << std::endl;
// 4. 检查是否存在:查找键6是否存在
if (numberMap.find(6) == numberMap.end())
{
std::cout << "键6不存在!" << std::endl;
}
// 5. 删除元素:删除键为2的条目
numberMap.erase(2);
// 6. 遍历所有元素(注意顺序是乱的!)
std::cout << "\n当前哈希表内容:" << std::endl;
for (const auto& pair : numberMap)
{
std::cout << "键" << pair.first << " => 值" << pair.second << std::endl;
}
return 0;
}
And the output of the program is here.
3对应的单词是: Three
键6不存在!
当前哈希表内容:
键5 => 值Five
键4 => 值Four
键3 => 值Three
键1 => 值One
This is the basic usage. Just learn how to use it.
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> roman = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
// do something here
}
};
And for the elegent way, just move the space.
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> roman = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
// do something here
}
};
and the program will know roman[I] = 1, roman[V] = 5.
if s = { III }, how to out put the number it represents?
- int n = s.size();
- for (int i = 0; i < n; i++){
- sum = sum + roman[s[i]]
![]() |
And for case 'IV', should return 4 instead of 6.
Think a way to work it out.
if s[i] < s[i + 1], like IV, the program should do 5 - 1;
if s[i] >= s{i + 1], like VI, the program should do 5 + 1;
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> roman = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
// do something here
int n = s.size();
int sum = 0;
for (int i = 0; i < n; i++)
{
if (roman[s[i]] < roman[s[i + 1]])
{
sum = sum - roman[s[i]];
}
else
{
sum = sum + roman[s[i]];
}
}
return sum;
}
};
![]() |
For my own, using unordered_map is not new. But I feels strange here. And after reviewing, it comes to me. Practise really works. I do like professioal people. No matter he is born with the gift or getting the gift through tranning. Smart is new sex.