当前位置:网站首页>【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,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- Detailed explanation of @accessories annotation of Lombok plug-in
- What if the win11 app store cannot load the page? Win11 store cannot load page
- LeetCode 2. Add two numbers
- Ap和F107数据来源及处理
- What is normal distribution? What is the 28 law?
- Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era
- Unity Json 编写
- 只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
- jsp 和 servlet 有什么区别?
- Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

一文看懂:数据指标体系的4大类型

La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter

Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation

Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements

DGraph: 大规模动态图数据集

LeetCode 2. Add two numbers

July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance

PWM controlled steering gear

Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)

【云原生】简单谈谈海量数据采集组件Flume的理解
随机推荐
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
你想要的宏基因组-微生物组知识全在这(2022.7)
js删除字符串中的子串
Classic quotations
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
[cloud native] briefly talk about the understanding of flume, a massive data collection component
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Where can I open computer administrator permissions
LeetCode 2. Add two numbers
大厂面试总结大全
What is generics- Introduction to generics
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
Hard core! One configuration center for 8 classes!
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)
618 deep resumption: Haier Zhijia's winning methodology
国内比较好的OJ平台[通俗易懂]
Yolov5 practice: teach object detection by hand
Résumé de l'entrevue de Dachang Daquan
pwm呼吸燈