leetcode-easy-math-13 Roman to Integer
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的更多相关文章
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- [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 ...
- LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
- 【leetcode❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 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 ,即 ...
- 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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【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 ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- python 短信邮件
短信邮件 hashlib- md5:非对称加密,不可逆的,经常用于加密密码然后存储- 示例: ```python import hashlib # 创建hash对象,可以指定需要加密的字符串 ...
- 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 ...
- Proxy.newInstance与InvocationHandler的使用示例
先定义一个接口,根据代理模式的原理,被代理类与代理类都要实现它. public interface Person { void eat(); } 再写一个实际执行任务的类(被代理类): public ...
- 新增分区格式化时提示设备文件不存在:--- No such file or directory的处理方法
[原文链接]:http://blog.itpub.net/28874898/viewspace-774249/ 在系统中的空余空间添加新的分区: fdisk /dev/sda (第一块硬盘上) ...
- TIOBE 7月排行:Python 过分炒作,Perl 成受害者?
与上个月相比,Python 的指数又增加了不少,由 8.530% 上升到 9.260%. 我们还留意到,TIOBE 对这期榜单的标题描述是“Perl is one of the victims of ...
- Beacon
1.Beacon技术指的是通过使用低功耗蓝牙技术(Bluetooth Low Energy,也就是Bluetooth 4.0或者Bluetooth Smart),Beacon基站便可以自动创建一个信号 ...
- c++ 指定目录下的文件遍历
要实现指定目录下文件的遍历需要执行一下的部分: 第一步获取当前路径的名字:(MAX_PATH是在windows定义的所有的路径名字不超过其,调用该函数会使得得到当前的目录) #include < ...
- 牛客练习赛46 C 华华跟奕奕玩游戏 (期望,概率)(详解)
链接:https://ac.nowcoder.com/acm/contest/894/C 来源:牛客网 华华跟奕奕玩游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...
- touch cyusbConfig.cmake
touch cyusbConfig.cmake cmake文件丢失,与其解决问题,不如临时建立一个临时文件
- 使用rpm安装mysql5.6(简单安装 实验使用)
[root@localhost mysql]# cd /usr [root@localhost mysql]# mkdir mysql [root@localhost mysql]# cd mysql ...