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 ...
随机推荐
- Pytorch报错记录
1.BrokenPipeError 执行以下命令时: a,b = iter(train_loader).next() 报错:BrokenPipeError: [Errno 32] Broken pip ...
- 027_git添加多账号设置
一. 注意事项: (1)公钥文件权限设置问题 现象: Permissions 0644 for '/Users/arunyang/.ssh/id_rsa_ele_me.pub' are too ope ...
- numpy数组扩展函数repeat和tile用法
numpy.repeat(a, repeats, axis=None) >>> a = np.arange(3) >>> a array([0, 1, 2]) &g ...
- 进入js
JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...
- Mysql基本架构及查询流程
mysql体系结构简单概述: Connectors:接入方,支持协议很多 Management Serveices & Utilities:系统管理和控制工具例如:备份恢复,mysql复制集群 ...
- LVS原理详解(3种工作方式8种调度算法)--老男孩
一.LVS原理详解(4种工作方式8种调度算法) 集群简介 集群就是一组独立的计算机,协同工作,对外提供服务.对客户端来说像是一台服务器提供服务. LVS在企业架构中的位置: 以上的架构只是众多企业里面 ...
- 在vue-cli3 中import引入一个没有export default{}的js文件
如果这个js文夹,放在vue-cli3中搭建的项目中的,public文件夹下,通过 //.js可以省略不行 import '/public/xxx.js' 其实你在浏览器中看的时候,发现会报错误 : ...
- 利用fastjson解析json并通过js&ajax实现页面的无跳转刷新
1.json是一种优秀的数据格式,在移动开发和web开发中经常用到,本例中通过一个小案例讲解如何通过alibaba的开源框架fastjson来解析jason数据格式并通过js实现无跳转刷新 2,新建一 ...
- 下面为初学者分享一下SQL 数据库学习资料
一.基础 1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份 ...
- bzoj5397 circular 随机化(
题目大意 给定一个环,环上有一些线段,试选出最多的线段 题解: 提醒:这可能是一篇非常欢乐的题解 我们考虑倍长环,然后断环为链 我们考虑枚举开头的线段,然后做一次贪心 这样子的复杂度根据实现的不同是\ ...