题目要求

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. jquery前端倒计时

    function FreshTime(){ // var endtime=new Date("2019/04/15,12:20:12");//结束时间 var endtime = ...

  2. Mysql --库和表的操作

    库的增删改查 系统数据库 创建数据库 数据库的相关操作 表的操作 存储引擎介绍(有点多 很啰唆) 表的介绍 表的操作 一.系统数据库 查看系统库: show databases; nformation ...

  3. Python-Django 模型层-多表查询

    多表操作 基于对象的跨表查询(多次查询) 一对一: -正向查询按字段 -反向查询按表名小写 一对多: -正向查询按字段(正向查询一定会查出一个来) -反向查询按表名小写_set.all()(返回结果是 ...

  4. 第一章:OEL6.8之虚拟机安装

    一.在   Windows 上安装  VMware Workstation 具体安装请参考<VMware Workstation 15 Pro 永久激活密钥 下载> 二.创建虚拟机 1:选 ...

  5. day13-迭代器及生成器

    iterable:可迭代的:iterator:迭代器: print(dir([ ]))  #打印出列表所拥有的所有方法: 可迭代协议:只要含有__iter__方法的都是可迭代的. 迭代器协议:含有__ ...

  6. element ui table单选框点击全选问题

    <template slot-scope="scope"> <el-radio-group v-model="scope.row.HandleState ...

  7. java中的try-catch-finally异常处理(学习笔记)

    一.异常概述 异常:Exception,是在运行发生的不正常情况. 原始异常处理: if(条件) { 处理办法1 处理办法2 处理办法3 } if(条件) { 处理办法4 处理办法5 处理办法6 } ...

  8. 关于getchar-scanf函数的相关坑!

    首先,我们编写如下所示的代码: #include <stdio.h> void test(int n) { ; ; ; a = b; b = c; c = n; printf(" ...

  9. 一次国际化记录以及平铺JSON数据

    ​ 写这个方法的原因是因为我们需要改版国际化,因为相同的项目有其他分支做过国际化,但是主版本没有进行过国际化,目前需要修改主版本的国际化,但是因为国际化的方式做了结构上的调整所以写了这个工具方法方便去 ...

  10. python基础一 ------排序和查找算法

    插入排序; 假设数组长度为n,先从第二个元素开始,与前一个元素比较,之后将较小的元素    放在前面,现在前两个元素是有顺序的,这时取第三个元素,与前一个元素(也就是第二个)比较,较小的放在前面   ...