LeetCode 13 Roman to Integer 解题报告
题目要求
Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
题目分析及思路
给一个罗马数字,要求得到它对应的整数。罗马数字与整数的对应关系是:'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000。罗马数字从左到右依次减小。需要考虑三种特殊情况:1)IV:4,IX:9;2)XL:40,XC:90;3)CD:400,CM:900。可以建一个字典存储这些罗马数字,之后可获得给定罗马数字各位所对应的整数列表,然后遍历这个列表,若右边的数字大于左边的数字,则需要加上右边的数字再减去两倍左边的数字;否则直接加上右边的数字。
python代码
class Solution:
def romanToInt(self, s: str) -> int:
d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
l = [d[i] for i in s]
ans = l[0]
for idx in range(1,len(l)):
if l[idx]>l[idx-1]:
ans += (l[idx]-2*l[idx-1])
else:
ans += l[idx]
return ans
LeetCode 13 Roman to Integer 解题报告的更多相关文章
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- LeetCode: Roman to Integer 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- [leetcode]13. Roman to Integer罗马数字转整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 13. Roman to Integer ☆☆
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- conda安装cv2库
conda install opencv-python或者 pip install opencv-python(不过好像是这个比较有效)
- $Django 支付宝支付,微信服务号推送消息 (测试需要把应用程序部署到服务器上)
一 支付宝支付 大概 支付宝支付 正式环境:需要用营业执照去申请商户号,appid 测试环境:沙箱环境:https://openhome.alipay.com/platform/appDaily.ht ...
- innodb文件损坏处理
innodb文件损坏报错如下: 2018-09-03T09:52:43.486363Z 0 [ERROR] InnoDB: Space id and page no stored in the pag ...
- Vue父子组件之间的相互通信
组件是Vue知识体系中最重要的一部分之一,父子组件由于作用域的不同,无法直接对对方的数据进行操作.它们之间的数据传递都是通过中间介质进行的,父组件给子组件传值是通过props属性,而子组件给父组件传值 ...
- tensorflow+ssd_mobilenet实现目标检测的训练
本文在Ubuntu下使用tensorflow的object detection API来训练自己的数据集.所用模型为ssd_mobilenet,也可以使用其他的模型.当然也可以在windows下训练, ...
- influxDB+grafana安装配置及邮件告警发送配置
1. InfluxDB安装 下载包并解压: $:wgethttps://dl.influxdata.com/influxdb/releases/influxdb-1.3.6_linux_amd64.t ...
- ionic 3 常见报错及解决办法
用ionic 3开发也有一段时间了,现在总结下开发中遇到的报错,以及解决办法: ERROR DOMException: Failed to execute 'setAttribute' on 'Ele ...
- 一致推崇的Linux系统还有那么安全吗?
今天想谈谈关于系统安全:我们都知道,Linux早已成为趋势,在我们互联网中占有不可或缺的地位,在我们眼中,它是神圣的,不可替代的,无懈可击的:真的是这样的吗? 但是关于病毒对Linux所造成的一系列威 ...
- CodeForces 286E Ladies' Shop 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8781889.html 题目传送门 - CodeForces 286E 题意 首先,给你$n$个数(并告诉你$m$ ...
- eclipse打开工作空间(workspace)没有任务反应
删除workspace下的.metadata文件夹,重新打开Eclipse就OK了.