当前位置:网站首页>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
边栏推荐
- Move blog to CSDN
- 19c RAC steps for modifying VIP and scanip - same network segment
- About me
- SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版
- Why do you need to learn container technology to engage in cloud native development
- sys. dbms_ scheduler. create_ Job creates scheduled tasks (more powerful and rich functions)
- 面试官给我挖坑:URI中的 “//” 有什么用?
- OSS cloud storage management practice (polite experience)
- Publish custom plug-ins to local server
- ARGB transparency conversion
猜你喜欢
Handling of high usage of Oracle undo
[point cloud series] learning representations and generative models for 3D point clouds
Campus takeout system - "nongzhibang" wechat native cloud development applet
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
零拷贝技术
零拷貝技術
SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
Android clear app cache
Interface idempotency problem
Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
随机推荐
Interface idempotency problem
[point cloud series] learning representations and generative models for 3D point clouds
Analysis of redo log generated by select command
联想拯救者Y9000X 2020
Interval query through rownum
[andorid] realize SPI communication between kernel and app through JNI
Analysis of unused index columns caused by implicit conversion of timestamp
MySQL and PgSQL time related operations
Why do you need to learn container technology to engage in cloud native development
Special window function rank, deny_ rank, row_ number
Modification of table fields by Oracle
The interviewer dug a hole for me: what's the use of "/ /" in URI?
解决tp6下载报错Could not find package topthink/think with stability stable.
[point cloud series] so net: self organizing network for point cloud analysis
Logstash数据处理服务的输入插件Input常见类型以及基本使用
Test the time required for Oracle library to create an index with 7 million data in a common way
Unified task distribution scheduling execution framework
Oracle index status query and index reconstruction
sys. dbms_ scheduler. create_ Job creates scheduled tasks (more powerful and rich functions)
10g database cannot be started when using large memory host