当前位置:网站首页>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
边栏推荐
- Oracle view related
- Get the attribute value difference between two different objects with reflection and annotation
- 19c RAC steps for modifying VIP and scanip - same network segment
- 校园外卖系统 - 「农职邦」微信原生云开发小程序
- Interval query through rownum
- [Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing
- Cross carbon market and Web3 to achieve renewable transformation
- 零拷貝技術
- Why do you need to learn container technology to engage in cloud native development
- Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
猜你喜欢
![[point cloud series] so net: self organizing network for point cloud analysis](/img/3c/6136b7aa322c42089f40ce13a2d747.png)
[point cloud series] so net: self organizing network for point cloud analysis

Lenovo Saver y9000x 2020

Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)

【重心坐标插值、透视矫正插值】原理以及用法见解

Interface idempotency problem

顶级元宇宙游戏Plato Farm,近期动作不断利好频频

TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例

Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
![MySQL [acid + isolation level + redo log + undo log]](/img/52/7e04aeeb881c8c000cc9de82032e97.png)
MySQL [acid + isolation level + redo log + undo log]
![MySQL index [data structure + index creation principle]](/img/11/6bdc8a62e977ffb67be07ded0c8978.png)
MySQL index [data structure + index creation principle]
随机推荐
Solution: you have 18 unapplied migration (s) Your project may not work properly until you apply
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling
Campus takeout system - "nongzhibang" wechat native cloud development applet
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版
JS time to get this Monday and Sunday, judge the time is today, before and after today
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
Detailed explanation of ADB shell top command
TCP 复位gongji原理和实战复现
Interval query through rownum
[multi screen interaction] realize dual multi screen display II: startactivity mode
RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
[point cloud series] multi view neural human rendering (NHR)
OSS cloud storage management practice (polite experience)
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
Ai21 labs | standing on the shoulders of giant frozen language models
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
./gradlew: Permission denied
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
切线空间(tangent space)