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
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:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can 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 ...
随机推荐
- sql server的sysobjects表中xtype字段值的含义
xtype字段包含的值的意思如下: AF = Aggregate function (CLR) 聚合函数(CLR) C = CHECK constraint CHECK约束 D = Default ...
- 我实在不懂Python的Asyncio
原语 事件循环(Event Loop) Awaitables和Coroutines Coroutine Wrappers Awaitables and Futures Tasks Handles Ex ...
- linux redis 主从复制
在从服务的redis.conf 添加 slaveof 主服务器 端口 查看reids进程和端口,都是存在的.只是ip地址是127.0.0.1而不是0.0.0.0,只是本机能使用; 查找redis的配置 ...
- VMware虚拟机安装Linux后忘记root密码怎么办(三)
第一种方法如下: 1.Linux开机 按键盘e今日GRUB界面如下:(GRUB管理引导启动盘) 切换到原系统目录: chroot /sysroot/ 2.重新启动客户机 3.使用新密码登录成功! 第二 ...
- matplotlib学习笔记
1.简介 matplotlib是python的一个2D绘图库,它可以在不同平台上地使用多种通用的绘图格式(hardcopy formats)和交互环境绘制出出版物质量级别的图片.matplotlib可 ...
- 解决使用maven的java web项目导入后出现的有关问题 -cannot be read or is not a valid ZIP file
解决使用maven的java web项目导入后出现的有关问题 -cannot be read or is not a valid ZIP file 错误问题:虽然查找repository目录下是有 ...
- Java Spring Boot VS .NetCore (七) 配置文件
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- RxJS操作符(二)
一.Observable的性质 三种状态:nex, error, complete 进入到Error状态: ) .filter(val=>{ throw '出错了' }) .take() .re ...
- 期货大赛项目|八,ueditor的应用
百度开发的富文本编辑器还是很不错的,可以已经不维护了 下载ueditor1_4_3_3-utf8-net放到项目中 找到net文件夹下config.json 修改以下两行 "imageUrl ...
- Servlet(九):web.xml文件和server.xml文件
Web.xml 文件使用总结:作用: 存储项目相关的配置信息,保护 Servlet.解耦一些数据对程序的依赖.使用位置: 每个 Web 项目中Tomcat 服务器中(在服务器目录 conf 目录中)区 ...