当前位置:网站首页>【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 !
边栏推荐
猜你喜欢

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

linux下配置Mysql授权某个用户远程访问,不受ip限制

Dgraph: large scale dynamic graph dataset

PCL point cloud image transformation

PWM控制舵机

Hard core! One configuration center for 8 classes!

Ranger (I) preliminary perception

Seal Library - installation and introduction

Kubernetes three open interfaces first sight

移动应用性能工具探索之路
随机推荐
What if the win11 app store cannot load the page? Win11 store cannot load page
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
MOSFET器件手册关键参数解读
PWM控制舵机
What is the difference between JSP and servlet?
DGraph: 大规模动态图数据集
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
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
Download blender on Alibaba cloud image station
Dgraph: large scale dynamic graph dataset
机器学习-感知机模型
lsf基础命令
基于Impala的高性能数仓实践之执行引擎模块
2322. 从树中删除边的最小分数(异或和&模拟)
LeetCode 1. 两数之和
Usage of sprintf() function in C language
IP地址转换地址段
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
Take you ten days to easily complete the go micro service series (I)