mycode  97.21%

class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
dic = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
res = 0
flag = 0
for i in range(len(s)-1):
if flag:
flag = 0
continue
temp = s[i] + s[i+1]
if temp in dic:
res += dic[temp]
flag = 1
else:
res += dic[s[i]]
if flag == 0:
return res + dic[s[-1]]
return res

参考

class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman_map = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
sum = 0
for idx, c in enumerate(s[:-1]):
num = roman_map[c]
sum += (-num) if roman_map[s[idx+1]] > num else num#题目已经说了字母是按大到小,除了特殊的
sum += roman_map[s[-1]]
return sum

leetcode-easy-math-13 Roman to Integer的更多相关文章

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

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

  2. [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 ...

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

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

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

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

  5. Leetcode 13. Roman to Integer(水)

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

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

  7. 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  ...

  8. 13. Roman to Integer【leetcode】

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

  9. 【LeetCode】13. Roman to Integer (2 solutions)

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

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

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

随机推荐

  1. 帝国cms 常用标签汇总

    1.列表内容标签 [!--empirenews.listtemp--]<!--list.var1-->[!--empirenews.listtemp--] 2.分页标签 [!--show. ...

  2. 【转载】java工程师学习之路---给自己的目标

    想学习或者提升java的可以看看,单从java角度来看总结的虽然还是很全面的,主要是为了自己看 http://blog.csdn.net/peace1213/article/details/50849 ...

  3. mybatis原理解析

    本文是结合spring-mybatis整合进行的分析 1.先看看依赖的jar包: <dependency> <groupId>org.mybatis</groupId&g ...

  4. idea安装完成后要做的几件事(设置字体、编码、行号)

    1.设置字体大小和样式 打开设置:File-->Settings 看到如下界面,输入font,点击Editor目录下的Font设置字体大小和样式: Font:字体样式 size:字体大小 Fal ...

  5. Js 将图片的绝对路径转换为base64编码(2)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. ceph问题汇总

    1. [ceph_deploy][ERROR ]RuntimeError: Failed to execute command: yum -y install epel-release 解决方案 进入 ...

  7. SQLServer中跨服务器跨数据库之间的数据操作

    首先必须理解一个概念: select * from sys.servers         (查看系统表,看原来的服务器名) 要想跨域就必须在以上信息中可以检索到! 怎样添加? --创建链接服务器  ...

  8. zabbix 添加 host item

    Zabbix常用术语 host(主机):监控的网络设备,可由IP或DNS名称指定. host Group(主机组):Host的逻辑容器,可以包含主机和模板. Item(监控项):一个特定监控指标的相关 ...

  9. java8学习之Stream分组与分区详解

    Stream应用: 继续举例来操练Stream,对于下面这两个集合: 需求是:将这两个集合组合起来,形成对各自人员打招呼的结果,输出的结果如: "Hi zhangsan".&quo ...

  10. 数字转化为汉字,如5->五

    //数字转化为汉字 如5-->五-(NSString*)translation:(NSString *)arebic{   NSString *str = arebic;    NSArray ...