当前位置:网站首页>【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 !
边栏推荐
- 【Leetcode】13. 罗马数字转整数
- Analysis of how to prevent virus in industrial computer
- LeetCode 4. 寻找两个正序数组的中位数(hard)
- How to solve the failure of printer driver installation of computer equipment
- Global and Chinese markets for disposable insulin pumps 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
- Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
- R and rstudio download and installation tutorial (super detailed)
- <四> H264解码输出yuv文件
- PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
猜你喜欢
OpenHarmony如何启动远程设备的FA
基于Impala的高性能数仓实践之执行引擎模块
渗透工具-内网权限维持-Cobalt strike
[cloud native] briefly talk about the understanding of flume, a massive data collection component
对接保时捷及3PL EDI案例
Day 18 of leetcode dynamic planning introduction
Understand one article: four types of data index system
Take you ten days to easily complete the go micro service series (I)
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
电脑自带软件使图片底色变为透明(抠图白底)
随机推荐
Analysis of how to prevent virus in industrial computer
What is the difference between JSP and servlet?
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
二、mock平台的扩展
P6774 [noi2020] tears in the era (block)
Talk about an experience of job hopping and being rejected
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
<四> H264解码输出yuv文件
[cloud native] briefly talk about the understanding of flume, a massive data collection component
Cloud native cicd framework: Tekton
IP address translation address segment
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
[fluent] dart data type boolean type (boolean type definition | logical operation)
Understand one article: four types of data index system
移动应用性能工具探索之路
基于多元时间序列对高考预测分析案例
Digital IC hand tearing code -- voting device
PWM breathing lamp
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share