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 ...
随机推荐
- Easy RM to MP3 Converter栈溢出定位及漏洞利用
本文主要是Easy RM to MP3 Converter(MFC++编写)栈溢出的定位及windows下shellcode编写的一些心得. 用到的工具及漏洞程序下载地址https://github. ...
- OpenCV3编程入门-读书笔记1
一.OpenCV概述 1.OpenCV全程Open Source Computer Vision Library,即开源计算机视觉库.它是一个跨平台的开源计算机视觉库,可以运行在windows.lin ...
- Visual studio 配置
解决方案 一个解决方案的文件结构: .sln 项目目录 debug release 其中,debug与release放置最终生成的dll或exe,项目目录下包含 头文 ...
- docker容器与宿主主机之间拷贝文件
文章链接:https://blog.csdn.net/libertine1993/article/details/80651552 https://blog.csdn.net/u011596455/a ...
- 强大的IDEA开发工具
开发工具切换IDEA 一:首先安装好IDEA工具并且配置maven信息 打开-File-Settings 新建maven WEB项目 打开-File-New-Project 点击NEXT 点击NEXT ...
- linux服务器配置pyspider出现Could not run curl-config 的解决方式
Downloading/unpacking pycurl (from pyspider) Downloading pycurl-7.19.5.1.tar.gz (142kB): 142kB downl ...
- ThreadLocal, HandlerThread, IntentService
1. ThreadLocal用法详解和原理https://www.cnblogs.com/coshaho/p/5127135.html // ThreadLocal methods: public T ...
- numpy的基础运算2-【老鱼学numpy】
numpy的基础运算中还有很多运算,我们这里再记录一些. 最小/大值索引 前面一篇博文中我们讲述过如何获得数组中的最小值,这里我们获得最小/大值的索引值,也就是这个最小/大值在整个数组中位于第几位. ...
- matplotlib注解-【老鱼学matplotlib】
本节讲述在图片中添加注解. 直接上代码: import numpy as np import pandas as pd import matplotlib.pyplot as plt # 生成x轴上的 ...
- cookie报错
错误: java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value 原因 ...