Roman numerals are represented by seven different symbols: IVXLCD and 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.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
 
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
return sum([roman[s[i]]*(-1 if roman[s[i]]<roman[s[i+1]] else 1) for i in range(len(s)-1)])+roman[s[-1]]

  

[LeetCode&Python] Problem 13. Roman to Integer的更多相关文章

  1. 【leetcode❤python】13. Roman to Integer

    #-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...

  2. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  3. Facebook interview problem:13. Roman to Integer

    description: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...

  4. LeetCode记录之13——Roman to Integer

    能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...

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

  6. Leetcode 13. Roman to Integer(水)

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

  7. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. C# 写 LeetCode easy #13 Roman to Integer

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

  9. 13. Roman to Integer【leetcode】

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

随机推荐

  1. Django之权限管理插件

    一.功能分析: 一个成熟的web应用,对权限的控制.管理是不可少的:对于一个web应用来说是什么权限? 这要从web应用的使用说起,用户在浏览器输入一个url,访问server端,server端返回这 ...

  2. 【转】EF 获取类的属性并排除特定属性(getType().GetProperties())

    当获取一个类型(class)的所有属性时,想排除指定属性,该如何操作? 比如:EF中一个实体类型UserEntity,通过反射获取这个类的属性时,想排除这个为映射的字段ID 使用以下方法即可! Pro ...

  3. nmon+nmon analyser安装使用教程

    nmon一般是两种用法,一是交互式用法查看实时的内存/cpu/网络/磁盘等情况,二是抓取一段时间内的实时的内存/cpu/网络/磁盘记到csv格式的.nmon文件中然后用nmon analyse做可视化 ...

  4. JQuery对象和DOM对象的区别与转换

    刚开始学习JQuery,经常分不清楚哪些是JQuery对象,哪些是DOM对象,了解它们之间的关系是很有必要的. 1.DOM对象和JQuery对象的区别 1)  DOM对象 DOM是Document O ...

  5. AI标尺,管理面板5.8

    拖动左上角的十字形,如图 重新定义坐标原点,双击十字形则恢复默认原点. 如果一个AI文件打开之后有多个画板,那么在重新存储的时候勾选“将每一个画板存储为单独的文件”那么每个画板都存储为单独的画板. “ ...

  6. LY.猜字小游戏

    猜字小游戏

  7. 关于java Collections.sort 排序

    public static void main(String[] args) { int[] dd = {12,34,46,123,23,2,35,13,543231,65,5645,57}; Arr ...

  8. linux系统监控与硬盘分区/格式化/文件系统管理

    1.系统监控 1) 系统监视和进程控制的工具----> Top 与  free      类似于windows的资源管理器.     进程运行的三种状态: tips: 进程(Process)是计 ...

  9. UVa 11636 - Hello World! 二分,水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  10. Kafka.net使用编程入门(一)

    最近研究分布式消息队列,分享下! 首先zookeeper  和 kafka 压缩包 解压 并配置好! 我本机zookeeper环境配置如下: D:\Worksoftware\ApacheZookeep ...