当前位置:网站首页>Leetcode brush question 𞓜 13 Roman numeral to integer
Leetcode brush question 𞓜 13 Roman numeral to integer
2022-04-23 13:45:00 【Du Xiaorui】
A problem I did three months ago , It has become today's daily question , It seems that if you don't record it, you will soon forget .
Title Description

Address : Roman numeral to integer
Solution 1
Although the title gives six special cases of Roman numerals to integers , But actually , The core of this question is , In the process of converting Roman array to integer , You need to observe the size relationship between the two numbers before and after , If the previous number is smaller than the following number , It takes so long to subtract the number in front of this , conversely , Just add this number . The logic is simple , Then let's correspond the numbers represented by each letter , Then you can traverse the given string .
class Solution:
def romanToInt(self, s: str) -> int:
prenum = self.getChar(s[0])
n = len(s)
num = 0
for i in range(1,n):
t = self.getChar(s[i])
if t > prenum:
num = num - prenum
else:
num = num + prenum
prenum = t
num += prenum
return num
def getChar(self, c):
if c == "I":
return 1
elif c == "V":
return 5
elif c == "X":
return 10
elif c == "L":
return 50
elif c == "C":
return 100
elif c == "D":
return 500
elif c == "M":
return 1000
else:
return 0
The correspondence between letters and numbers can also be directly used in the dictionary :
class Solution(object):
def romanToInt(self,s):
""" :type s: str :rtype: int """
num_dict = {
'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
sum = 0
prenum = num_dict[s[0]]
for i in range(1,len(s)):
num = num_dict[s[i]]
if prenum<num:
sum = sum - prenum
else:
sum = sum + prenum
prenum = num
return sum + prenum
版权声明
本文为[Du Xiaorui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230556585094.html
边栏推荐
- Analysis of unused index columns caused by implicit conversion of timestamp
- 10g database cannot be started when using large memory host
- JS compares different elements in two arrays
- 【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享
- [point cloud series] full revolutionary geometric features
- JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
- Oracle RAC database instance startup exception analysis IPC send timeout
- 集简云 x 飞书深诺,助力企业运营部实现自动化办公
- Troubleshooting of expdp export error when Oracle table has logical bad blocks
- Ora-16047 of a DG environment: dgid mismatch between destination setting and target database troubleshooting and listening vncr features
猜你喜欢

PG SQL intercepts the string to the specified character position

Cross carbon market and Web3 to achieve renewable transformation

零拷貝技術
![[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling](/img/da/02d1e18400414e045ce469425db644.png)
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling

The interviewer dug a hole for me: what's the use of "/ /" in URI?

Static interface method calls are not supported at language level '5'

10g database cannot be started when using large memory host

Search ideas and cases of large amount of Oracle redo log

Unified task distribution scheduling execution framework

TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
随机推荐
Core concepts of microservice architecture
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
Detailed explanation and usage of with function in SQL
GDB的使用
TCP reset Gongji principle and actual combat reproduction
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling
Processbuilder tool class
Logstash数据处理服务的输入插件Input常见类型以及基本使用
PG library to view the distribution keys of a table in a certain mode
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
Generate 32-bit UUID in Oracle
Explanation of input components in Chapter 16
Oracle kills the executing SQL
10g database cannot be started when using large memory host
19c environment ora-01035 login error handling
Aicoco AI frontier promotion (4.23)
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
Set Jianyun x Feishu Shennuo to help the enterprise operation Department realize office automation
Test the time required for Oracle library to create an index with 7 million data in a common way
Oracle RAC database instance startup exception analysis IPC send timeout