题目要求

Roman numerals are represented by seven different symbols: IVXLCDand 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 before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (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 解题报告的更多相关文章

  1. 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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. LeetCode: Roman to Integer 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

  4. 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 ...

  5. [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 ...

  6. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  7. LeetCode 13. Roman to Integer(c语言版)

    题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. crowdstrike提供的应急响应工具

    下载链接 https://www.crowdstrike.com/resources/community-tools/ CROWDSTRIKE防病毒资源监视器 CrowdStrike Antiviru ...

  2. ES进阶--04

    第30节彻底掌握IK中文分词_上机动手实战IK中文分词器的安装和使用 之前大家会发现,我们全部是用英文在玩儿...好玩儿不好玩儿...不好玩儿 中国人,其实我们用来进行搜索的,绝大多数,都是中文应用, ...

  3. 【原创】大数据基础之Ambari(4)通过Ambari部署Impala

    ambari2.7.3(hdp3.1) 安装 impala2.12(自动安装最新) ambari的hdp中原生不支持impala安装,下面介绍如何通过mpack方式使ambari支持impala安装: ...

  4. 入坑C++

    c++中的++来自c语言中的递增运算符,该运算符将变量加1,c++起初也叫c with class ,通过通过名称表面,C++是对c的扩展,因此C++是c语言的超集,这以为这任何有效的c程序都是有效的 ...

  5. ARKit1.5 采坑

    1.对应的生成的预制体,0.1的大小按照Cube的实际大小进行缩放. Plane和Cube都是0.1的情况下是不一样的大小的.

  6. ECMAScript6 - 2.变量的解构赋值

    1.数组解构赋值 1.1.基本用法 // (1)对数组变量赋值 let [foo, [[bar], baz]] = [1, [[2], 3]]; foo; // 1 bar; // 2 baz; // ...

  7. jQuery的下拉选select2插件用法

    1转自:https://www.jb51.net/article/95561.htm 用了这么久的Select2插件,也该写篇文章总结总结.当初感觉Select2不是特别好用,但又找不到比它更好的下拉 ...

  8. 2018-2019-1 20189201 《LInux内核原理与分析》第八周作业

    只有在天足够黑的时候你才能看到星星. BY WAY GK 加油 一.书本第七章知识总结[可执行程序工作原理] 1. ELF目标文件格式 ELF全称Executable and Linkable For ...

  9. LeetCode 709.To Lower Case

    Description Implement function ToLowerCase() that has a string parameter str, and returns the same s ...

  10. Pok 使用指南

    Pok 使用指南 POK 是一个开源的符合ARINC653的操作系统,因为一些原因,我要开始接触一个全新的领域,再此希望记录下每天点滴进步,同时也欢迎指正吧. 目前先简单说明POK的使用指南 获取源码 ...