当前位置:网站首页>【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 !
边栏推荐
- P6774 [noi2020] tears in the era (block)
- Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
- 如何与博格华纳BorgWarner通过EDI传输业务数据?
- DGraph: 大规模动态图数据集
- Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
- linux下配置Mysql授权某个用户远程访问,不受ip限制
- 2、 Expansion of mock platform
- 对接保时捷及3PL EDI案例
- LeetCode 3. 无重复字符的最长子串
- uboot的作用和功能
猜你喜欢

LeetCode 2. Add two numbers

Résumé de l'entrevue de Dachang Daquan

如何与博格华纳BorgWarner通过EDI传输业务数据?

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

机器学习-感知机模型

配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)

TCP server communication process (important)

DigiCert SSL证书支持中文域名申请吗?

Interview summary of large factories
![[error record] error -32000 received from application: there are no running service protocol](/img/6c/66099650de46cac88b805e6cfb90b9.jpg)
[error record] error -32000 received from application: there are no running service protocol
随机推荐
< IV & gt; H264 decode output YUV file
The macrogenome microbiome knowledge you want is all here (2022.7)
LeetCode 4. 寻找两个正序数组的中位数(hard)
PWM breathing lamp
入行数字IC验证后会做些什么?
What is generics- Introduction to generics
基于Impala的高性能数仓实践之执行引擎模块
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
Cloud native cicd framework: Tekton
Résumé de l'entrevue de Dachang Daquan
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
TCP congestion control details | 2 background
linux下配置Mysql授权某个用户远程访问,不受ip限制
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
OpenHarmony如何启动远程设备的FA
pwm呼吸灯
Domestic relatively good OJ platform [easy to understand]
学习周刊-总第60期-2022年第25周
Day 18 of leetcode dynamic planning introduction
如何与博格华纳BorgWarner通过EDI传输业务数据?