【Leetcode】 13. 罗马数字转整数

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').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

Thought:

  1. 定义一个哈希表,将罗马数字的每个字符映射到对应的整数值上。
  2. 初始化一个变量 ans0,表示最终的整数值
  3. 遍历输入的罗马数字字符串 s,对于每个字符
    a. 获取其对应的整数值 value
    b. 如果当前字符不是字符串的最后一个字符,且其对应的整数值小于其后面字符对应的整数值,则将 ans 减去 value
    c. 否则将 ans 加上 value
  4. 遍历完成后,返回 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

【Leetcode】 13. 罗马数字转整数_第1张图片


Supplement:
C++标准库提供了unordered_map容器,用于存储键-值对。下面介绍unordered_map的使用方法。

  1. 头文件
#include 
  1. 定义unordered_map
std::unordered_map<std::string, int> my_map;

上面定义了一个unordered_map,键为string类型,值为int类型。

  1. 插入元素
my_map.insert({"apple", 3});
my_map["banana"] = 5;

支持使用insert函数和[]运算符来插入元素。

  1. 访问元素
std::cout << my_map["apple"] << std::endl;

可以通过[]运算符访问元素,如果该键不存在,则会自动插入一个默认值。

  1. 遍历元素
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
    std::cout << it->first << ": " << it->second << std::endl;
}

使用迭代器遍历元素,其中it->first表示键,it->second表示值。

  1. 删除元素
my_map.erase("apple");

使用erase函数删除元素。

  1. 判断元素是否存在
if (my_map.find("apple") != my_map.end()) {
    std::cout << "The element is found." << std::endl;
}

使用find函数判断元素是否存在,如果该键存在,则返回该键对应的迭代器,否则返回end()迭代器。

  1. 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的基本操作方法。

你可能感兴趣的:(Leetcode,leetcode,算法,c++)