[LeetCode&Python] Problem 13. Roman to Integer
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
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 beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(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的更多相关文章
- 【leetcode❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- 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 ...
- LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
- 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 ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- Linux登录超时自动退出处理办法
出于安全方面的考虑,机器常要求配置一个登录时间期限,当闲置超过这一期限就自动退出:但在某些场合我们需要时不时地就使用机器,如果每次都要重新ssh登录那是非常麻烦的 方法一:让当前会话一直处于工作状态 ...
- X-Window/GNOME/KDE的关系
名称 开发者 开发年份 许可证 语言 说明 X-Window MIT 1984 X-Window是一个C/S架构的桌面框架,KDE和GNOME都是其基础上的具体实现 KDE Trolltech ...
- prefix word se sub suc sup suf sur out~s
1★ se 区分开,分开 ,离开 2★ sub 接近,靠近,次一等 ,次的 3★ suc 4★ sup 5★ suf 6★ sur 在~下面
- Oracle的创建表和创建约束的Sql语句
Oracle的创建表和创建约束的Sql语法 1.创建表的语句 ---1.创建模拟的数据表 --- --1.1.创建学生表Student create table Student( StuId NUMB ...
- react router @4 和 vue路由 详解(四)vue如何在路由里面定义一个子路由
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 6.vue如何在路由里面定义一个子路由? 给父路由加一个 children:[] 参考我 ...
- 基础数据类型的坑和集合及深浅copy
一.基础数据类型的坑: 元组: 如果一个元组中,只有一个元素,且没有逗号,则该"元组"与里面的数据的类型相同. # 只有一个数据,且没有逗号的情况: print(tu1,type( ...
- 逆袭之旅DAY28.XIA.异常处理
2018-07-24 14:42:24 第一种: 第二种: 第三种: 执行 try--catch--finally--return(执行return 退出方法) 代码示例: 输入数字,输出对应课 ...
- linux用户管理 查看用户信息
LINUX系统中用户登录查看命令 W命令 [root@ssgao1987 ~]# w 04:57:01 up 11:50, 2 users, load average: 0.00, 0.00, 0 ...
- python 学习 map /reduce
python 内建了map()和reduce()函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. ...
- bzoj1294
题解: 首先发现假如一个豆豆被多边形围住了,那么从这个豆豆引出一条射线 会有奇数个焦点 然后我们从每个豆豆引出一条射线 然后状压dfs 代码: #include<bits/stdc++.h> ...