当前位置:网站首页>【Leetcode】13. 罗马数字转整数
【Leetcode】13. 罗马数字转整数
2022-07-02 14:18:00 【wangzirui32】
博文作者 wangzirui32
喜欢的可以 点赞 收藏 关注哦~~
本文首发于CSDN,未经许可禁止转载
1. 题目描述

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/roman-to-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题思路
我们可以创建一个字典,将每单个罗马数字与阿拉伯数字作为键值对,构成罗马数字表,然后使用for循环键值对(k和v),检测字符串s中存在几个k, 有一个k就有一个v,可以使用乘法计算,然后去除字符串中所有k,然后,进入下一次循环,最后返回结果。
3. 代码实现
Code:
class Solution:
def romanToInt(self, s: str) -> int:
"""罗马数字转整数"""
"""注意:这里IV,IX等特殊的罗马数字在字典的前面,这是为了避免转换错误。"""
roman_table = {
"IV": 4,
"IX": 9,
"XL": 40,
"XC": 90,
"CD": 400,
"CM": 900,
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
result = 0 # 结果
for k, v in roman_table.items(): # 遍历字典
result += s.count(k) * v # 查询k的数量 * k所代表的值(v)
s = s.replace(k, "") # 替换k 避免重复计算
return result
4. 最终结果

好了,今天的课程就到这里,我是wangzirui32,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- js删除字符串中的子串
- Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
- Classic quotations
- Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
- P6774 [NOI2020] 时代的眼泪(分块)
- Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
- OpenHarmony如何启动远程设备的FA
- vscode设置删除行快捷键[通俗易懂]
- PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
猜你喜欢

机器学习-感知机模型

只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!

串口控制舵机转动

TCP server communication process (important)

Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)

Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品

PWM breathing lamp

LeetCode 2. 两数相加

Routing mode: hash and history mode

Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
随机推荐
Exploration and practice of integration of streaming and wholesale in jd.com
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
[fluent] dart data type boolean type (boolean type definition | logical operation)
What is the difference between JSP and servlet?
TCP congestion control details | 2 background
LeetCode 5. Longest Palindromic Substring
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
What is generics- Introduction to generics
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
什么是泛型?- 泛型入门篇
LeetCode 2. 两数相加
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
LeetCode 1. Sum of two numbers
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
[cloud native] briefly talk about the understanding of flume, a massive data collection component
PWM controlled steering gear
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
LeetCode 3. Longest substring without duplicate characters
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)