题目描述:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

解题思路:

首先我们要了解罗马数字怎么写的

个位数举例

I, 1 】II, 2】 III, 3】 IV, 4 】V, 5 】VI, 6】 VII, 7】 VIII,8 】IX, 9

·十位数举例

X, 10】 XI, 11 】XII, 12】 XIII, 13】 XIV, 14】 XV, 15 】XVI, 16 】XVII, 17 】XVIII, 18】 XIX, 19】 XX, 20】 XXI, 21 】XXII, 22 】XXIX, 29】 XXX, 30】 XXXIV, 34】 XXXV, 35 】XXXIX, 39】 XL, 40】 L, 50 】LI, 51】 LV, 55】 LX, 60】 LXV, 65】 LXXX, 80】 XC, 90 】XCIII, 93】 XCV, 95 】XCVIII, 98】 XCIX, 99 】

·百位数举例

C, 100】 CC, 200 】CCC, 300 】CD, 400】 D, 500 】DC,600 】DCC, 700】 DCCC, 800 】CM, 900】 CMXCIX,999】

·千位数举例

M, 1000】 MC, 1100 】MCD, 1400 】MD, 1500 】MDC, 1600 】MDCLXVI, 1666】 MDCCCLXXXVIII, 1888 】MDCCCXCIX, 1899 】MCM, 1900 】MCMLXXVI, 1976】 MCMLXXXIV, 1984】 MCMXC, 1990 】MM, 2000 】MMMCMXCIX, 3999】

看到上面我们就发现,其实最难办的就是像IV这样的需要判断一下,其他的直接累加就好,所以我们可以把这些需要两个一起的也当做基础数字来减少判断。

class Solution:
# @return an integer
def romanToInt(self, s):
dic = {"M":1000,"CM":900,"D":500,"CD":400,"C":100,"XC":90,"L":50,"XL":40,"X":10,"IX":9,"V":5,"IV":4,"I":1}
l = len(s)
res = 0
i = 0
while i < l:
if i+1 < l and s[i]+s[i+1] in dic:
res += dic[s[i]+s[i+1]]
i += 1
else:
res += dic[s[i]]
i += 1
return res

【leetcode】Roman to Integer的更多相关文章

  1. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  2. 【LeetCode】Roman to Integer(罗马数字转整数)

    这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...

  3. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

    12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...

  4. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  5. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  6. 【Leetcode】【Easy】Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  7. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  9. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

随机推荐

  1. 12月15日smarty模板基本语法

    smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...

  2. webapi8

  3. [Unity3D]UI界面之瞄准镜设置说明

    9空格设计 : 比如说4个角的图案固定,拉伸的时候不受影响 通过设置 左上右下来: 通过创建Image对象,将设置好的图片关联到Source Image 调整瞄准镜跟随飞机, 注意这里设置的Z轴向量是 ...

  4. Oracle to_date()函数的用法

    Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...

  5. Android 只开启一个Activity实例

    在一个Activity中,多次调用startActivity()来启动另一个Activity,要想只生成一个Activity实例,方法有两种. 方法一:设置起动模式 一个Activity有四种启动模式 ...

  6. scrollView滚动原理

    首先要明确的是,scrollview 其实和普通的 view 并没有多大的差别,只不过给它加上了一些手势和约定. 我们知道,要让一个 scrollview 能够滚动的方法是设置它的 contentSi ...

  7. bootstrap的table调用本列ID

    我们是用json解析数据. 后台传送data数据~ String data = JSON.toJSONString(baseInfoService.list());request.setAttribu ...

  8. jquery validate minlength rule is not working

    Question: I have a form with a password field. The password has to be at least 8 characters long. &l ...

  9. linux c/c++

    string 字符串操作 操作数的都是 ( char * )型,操作数必须是指向字符串的指针("a"),不能是字符('a'),操作时不考虑末尾的'\0'. size_t strle ...

  10. CentOS7 睡眠 休眠 关机 电源

    设置装有 CentOS7 的笔记本合盖后黑屏进入睡眠模式 systemd 能够处理某些电源相关的 ACPI事件,你可以通过从 /etc/systemd/logind.conf 以下选项进行配置: Ha ...