mycode  97.21%

  1. class Solution(object):
  2. def romanToInt(self, s):
  3. """
  4. :type s: str
  5. :rtype: int
  6. """
  7. 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}
  8. res = 0
  9. flag = 0
  10. for i in range(len(s)-1):
  11. if flag:
  12. flag = 0
  13. continue
  14. temp = s[i] + s[i+1]
  15. if temp in dic:
  16. res += dic[temp]
  17. flag = 1
  18. else:
  19. res += dic[s[i]]
  20. if flag == 0:
  21. return res + dic[s[-1]]
  22. return res

参考

  1. class Solution(object):
  2. def romanToInt(self, s):
  3. """
  4. :type s: str
  5. :rtype: int
  6. """
  7. roman_map = {
  8. "I": 1,
  9. "V": 5,
  10. "X": 10,
  11. "L": 50,
  12. "C": 100,
  13. "D": 500,
  14. "M": 1000
  15. }
  16. sum = 0
  17. for idx, c in enumerate(s[:-1]):
  18. num = roman_map[c]
  19. sum += (-num) if roman_map[s[idx+1]] > num else num#题目已经说了字母是按大到小,除了特殊的
  20. sum += roman_map[s[-1]]
  21. 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. python 短信邮件

    短信邮件 hashlib​- md5:非对称加密,不可逆的,经常用于加密密码然后存储​- 示例:​ ```python import hashlib ​ # 创建hash对象,可以指定需要加密的字符串 ...

  2. Spark报错java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

    Spark 读取 JSON 文件时运行报错 java.io.IOException: Could not locate executable null\bin\winutils.exe in the ...

  3. Proxy.newInstance与InvocationHandler的使用示例

    先定义一个接口,根据代理模式的原理,被代理类与代理类都要实现它. public interface Person { void eat(); } 再写一个实际执行任务的类(被代理类): public ...

  4. 新增分区格式化时提示设备文件不存在:--- No such file or directory的处理方法

    [原文链接]:http://blog.itpub.net/28874898/viewspace-774249/ 在系统中的空余空间添加新的分区:   fdisk   /dev/sda (第一块硬盘上) ...

  5. TIOBE 7月排行:Python 过分炒作,Perl 成受害者?

    与上个月相比,Python 的指数又增加了不少,由 8.530% 上升到 9.260%. 我们还留意到,TIOBE 对这期榜单的标题描述是“Perl is one of the victims of ...

  6. Beacon

    1.Beacon技术指的是通过使用低功耗蓝牙技术(Bluetooth Low Energy,也就是Bluetooth 4.0或者Bluetooth Smart),Beacon基站便可以自动创建一个信号 ...

  7. c++ 指定目录下的文件遍历

    要实现指定目录下文件的遍历需要执行一下的部分: 第一步获取当前路径的名字:(MAX_PATH是在windows定义的所有的路径名字不超过其,调用该函数会使得得到当前的目录) #include < ...

  8. 牛客练习赛46 C 华华跟奕奕玩游戏 (期望,概率)(详解)

    链接:https://ac.nowcoder.com/acm/contest/894/C 来源:牛客网 华华跟奕奕玩游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...

  9. touch cyusbConfig.cmake

    touch cyusbConfig.cmake cmake文件丢失,与其解决问题,不如临时建立一个临时文件

  10. 使用rpm安装mysql5.6(简单安装 实验使用)

    [root@localhost mysql]# cd /usr [root@localhost mysql]# mkdir mysql [root@localhost mysql]# cd mysql ...