【leetcode】Roman to Integer
题目描述:
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的更多相关文章
- 【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 ...
- 【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 ...
- 【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 ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- 【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 ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
随机推荐
- React项目(二):生命游戏
引子 这是16年最后的一个练手项目,一贯的感觉就是,做项目容易,写说明文档难.更何况是一个唤起抑郁感觉的项目,码下的每个字,心就如加了一个千斤的砝码. 2016年,有些事我都已忘记,但我现在还记得.2 ...
- Mac Brew Install Nginx Summary
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.1.el_capitan.bot################# ...
- C语言产生标准正态分布或高斯分布随机数
C语言 产生标准正态分布或高斯分布 随机数 产生正态分布或高斯分布的三种方法: 1. 运用中心极限定理(大数定理) #include #include #define NSUM 25 double g ...
- runtime-对成员变量和属性的操作
成员变量 首先我们来看看成员变量在runtime中是什么样的 在runtime中成员变量是一个objc_ivar类型的结构体,结构体定义如下 struct objc_ivar { char *ivar ...
- Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 拒绝访问。
这几天在写一个导出word的功能,使用 Microsoft.Vbe.Interop.dll和Office.dll 在本地都可以正常运行,但是上传到服务器后就报错,如下图: 对于此问题,也在网上查了一些 ...
- phpstorm 使用技巧
专题1 专题2 专题3 专题4 快捷键
- for和foreace的区别
foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本,但是foreach语句并不能完全取代for语 ...
- [Storm] No data flows into bolt
最近在HDP2.1的HBase环境中安装了一个Storm测试机器(单节点,JDK8),遇到了几个问题,记录下来. 尝试步骤 1. 使用和HBase一样HDP版本,直接安装Storm yum insta ...
- nginx使用ngx_lua访问后端Thrift-Server实现和介绍
背景 随着openresty的出现,让nginx使用lua解决一些业务的能力大幅度提高,ngx_lua可以使用nginx自生的基于事件驱动的IO模型,和后端的存储,业务等系统实现非阻塞的连接交互. 如 ...
- 微信小程序导航:官方工具+精品教程+DEMO集合(1月7更新)
1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=14764346784612:简易教程:https://mp.weixin.qq.com/debug ...