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. 03 Linux下运行Django项目

    1.安装windows和linux传输文件的工具 pip install lrzsz 提供两个命令 一个是上传一个是下载 rz 接收 直接rz sz 上传 直接sz 或者直接拖拽 2.在线下载资源的命 ...

  2. Centos7下安装ZooKeeper

    1.下载源码 zookeeper 需要jdk的支持,需要先安装jdk 官网下载地址: http://mirrors.hust.edu.cn/apache/zookeeper/ 选择最新的版本进行下载 ...

  3. leetcode297. 二叉树的序列化与反序列化

    代码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...

  4. Nginx(web服务器)与Tomcat(应用服务器)搭建集群

    Nginx作为互联网最常用的web服务器,高性能的HTTP和反向代理使它经常作为Tomcat集群的方案.Nginx官方只支持使用HTTP协议的集成,但是如果你想使用AJP协议集成,可以使用阿里开源的n ...

  5. 六,k8s集群service资源

    目录 Service简介 ClusterIP Headless(无头service) NodePort Service简介 service的基本说明: Service 是作用于客户端可服务端(Pod) ...

  6. python分别获取虚拟网卡和真实网卡ip

    #!/usr/bin/python # -*- coding: utf-8 -*- import commands import socket import fcntl import struct C ...

  7. HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online

    N个数(N<=100000),M个询问,每次询问L,R,求F(L,R). F(L,R)=F(L,R-1)%A[R] , L<R 这道题数据比较鶸 可以直接用递减爆 正确做法应该是倍增 用倍 ...

  8. 关于join

  9. 程序中的一些限制(基于Linux系统C语言)

    今天突然想起来几个问题,在程序运行起来时,存在一些限制: 1,数组的长度(成员的个数)存在限制!(数组定义的空间大小)2,一个进程里打开的文件数.3,一个文件的名字的长度.4,一个进程里创建线程的个数 ...

  10. 第一次把本地项目与git相连

    原文:https://blog.csdn.net/a987625922/article/details/82189863 新建远程仓库(github或者gitee) 将本地仓库转换成版本库,并将文件添 ...