当前位置:网站首页>【Leetcode】13. Roman numeral to integer
【Leetcode】13. Roman numeral to integer
2022-07-02 17:01:00 【wangzirui32】
Bowen author wangzirui32
Like can give the thumbs-up Collection Pay attention to ~~
This article was first published in CSDN, Reprint is prohibited without permission
1. Title Description

source : Power button (LeetCode)
link :https://leetcode.cn/problems/roman-to-integer
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Their thinking
We can create a dictionary , Take each Roman numeral and Arabic numeral as key value pairs , Form Roman numeral table , And then use for Circular key value pair (k and v), Detect string s There are several k, There is one k There is one v, You can use multiplication , Then remove all k, then , Enter next cycle , Last result returned .
3. Code implementation
Code:
class Solution:
def romanToInt(self, s: str) -> int:
""" Roman numeral to integer """
""" Be careful : here IV,IX Wait for special Roman numerals in front of the dictionary , This is to avoid conversion errors ."""
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 # result
for k, v in roman_table.items(): # Ergodic dictionary
result += s.count(k) * v # Inquire about k The number of * k The value represented (v)
s = s.replace(k, "") # Replace k Avoid double counting
return result
4. final result

Okay , That's all for today's lesson , I am a wangzirui32, You can collect and pay attention to what you like , See you next time !
边栏推荐
- 基于多元时间序列对高考预测分析案例
- ⌈ 2022 ⌋ how to use webp gracefully in projects
- 小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
- What is generics- Introduction to generics
- 2322. 从树中删除边的最小分数(异或和&模拟)
- 关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
- Seal Library - installation and introduction
- Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- Seven charts, learn to do valuable business analysis
- 流批一体在京东的探索与实践
猜你喜欢

基于多元时间序列对高考预测分析案例

亚马逊云科技 Community Builder 申请窗口开启

linux安装postgresql + patroni 集群问题

Hard core! One configuration center for 8 classes!

Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
![john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])

大厂面试总结大全

【云原生】简单谈谈海量数据采集组件Flume的理解

Serial port controls steering gear rotation

How to choose the right kubernetes storage plug-in? (09)
随机推荐
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
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
IP地址转换地址段
AcWing 300. Task arrangement
Analysis of how to prevent virus in industrial computer
Where can I open computer administrator permissions
Learning Weekly - total issue 60 - 25th week of 2022
vscode设置删除行快捷键[通俗易懂]
机器学习-感知机模型
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
【云原生】简单谈谈海量数据采集组件Flume的理解
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
Lampe respiratoire PWM
How openharmony starts FA of remote devices
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
VMware install win10 image
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Student course selection system (curriculum design of Shandong Agricultural University)
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors