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. vue--微信支付

    1.当前页面没有注册: 需要登录微信支付商家后台,添加支付路径授权 2.安装 weixin-js-sdk 微信SDK npm install weixin-js-sdk --save 3.引入 imp ...

  2. 谷歌浏览器(Chrome)离线包的下载方法!

    谷歌浏览器(Chrome)其实可以下载离线包,用离线包安装的好处,就是一次获得全部安装文件,不需要漫长的在线下载过程了! 下载地址:https://www.google.com/chrome/eula ...

  3. (转)Python3.X如何下载安装urllib2包 ?

    python 3.X版本是不需要安装:urllib2包的,urllib和urllib2包集合成在一个包了那现在问题是:在python3.x版本中,如何使用:urllib2.urlopen()? 答:i ...

  4. MySQL之RPM安装说明及配置

    1.查看当前系统是否安装过Linux rpm -qa | grep -i mysql 未安装无任何输出:安装会打印对应mysql的rpm安装包. 2.准备安装包: MySQL-client-5.5.4 ...

  5. C语言 STL中qsort用法

    qsort函数包含在<stdlib.h>的头文件里. qsort函数声明如下:void qsort(void *base, size_t nmemb, size_t size, int(* ...

  6. Ceph实战入门之安部署篇

    最近Ceph官方发布了luminous长久支持版,新版本增加了很多有意思的功能,但是入门还是先从部署安装开始. 环境说明 在Win10下安装VMware® Workstation 12 Pro软件,用 ...

  7. 小米Air安装Arch Linux之图形界面配置(Gnome 和 sway)持续更新中……

    0. 前言 上一篇文章简单讲述了在小米Air上安装Arch Linux的经验,但是安装完后基本系统后,还需要额外的配置才能进到日常使用.下文简单列举一些步骤. 1. 参考网站 主要还是参考ARCH W ...

  8. Backtracking(一)

    LeetCode中涉及到回溯的题目有通用的解题套路: 46. permutations 这一类回溯题目中的基础中的基础,无重复数字枚举: /* Given a collection of distin ...

  9. 多线程-生产者消费者(BlockingQueue实现)

    三.采用BlockingQueue实现 BlockingQueue也是java.util.concurrent下的主要用来控制线程同步的工具. BlockingQueue有四个具体的实现类,根据不同需 ...

  10. 一个简单的c++类的定义和实例化

    #include "iostream" #include <string> using namespace std; class mycoach { private: ...