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. Centos7:配置防火墙

    firewalld的基本使用 启动: systemctl start firewalld 关闭:systemctl stop firewalld 查看状态: systemctl status fire ...

  2. C# 递归式快速排序算法

    static void Main(string[] args) { Console.WriteLine("************快速排序*****************"); ...

  3. c++ 用模板类实现顺序储存的线性表

    首先要注意的一点是模板类在VS中编译时如果将定义和声明分开会出现无法解析的问题,所以一般比较常见的解决的办法是将声明和定义放在同一个头文件中然后统一的调用,下面就是用模板类实现线性表的编写 #prag ...

  4. 跟着动画来学习TCP三次握手和四次挥手

    TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入一点,他们往往都无法作出准确回答. 点我查看如何应对面试中的三次握手.四次挥手 本篇尝试使用动画来对 ...

  5. maven中使用jetty插件

    <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin ...

  6. LeetCode_Bit Manipulation

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  7. 次小生成树(LCA倍增)

    算法: 求出MST之后枚举每条在MST之外的边 连上之后会出现环 找到环中除加上的边之外权值最大的边 删除该边之后得到一颗新树 做法: 利用LCA倍增地维护最小生成树上两点之间的最大边权 每次枚举在M ...

  8. 【BZOJ1049】【Luogu P2501】 [HAOI2006]数字序列 DP,结论,LIS

    很有(\(bu\))质(\(hui\))量(\(xie\))的一个题目. 第一问:求最少改变几个数能把一个随机序列变成单调上升序列. \(Solution:\)似乎是一个结论?如果两个数\(A_i\) ...

  9. 使用SpringAOP实现事务(声明式事务管理、零配置)

    前言: 声明式事务管理建立在AOP之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务.声明式事务最大的优点就是不需要通过编 ...

  10. DOM导航与DOM事件

    HTML DOM 导航 通过 HTML DOM,能够使用节点关系在节点树中导航. ㈠HTML DOM 节点列表 getElementsByTagName() 方法返回节点列表.节点列表是一个节点数组. ...