LeetCode-13罗马数字转整数
2018-03-21 09:58
435 查看
"""
罗马数字只有7个字母组成,每个字母代码的字如下:
ROMAN = {'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1}
题目hint1中给出,注意格式即可
没有明确给出罗马数字与阿拉伯数字的转换关系,已有总结如下:四个规则(http://blog.csdn.net/net_wolf_007/article/details/51770112)相同的数字连写, 所表示的数等于这些数字相加得到的数。如 XXX表示 30
小的数字在大的数字的右边, 所表示的数等于这些数字相加得到的数 如VIII 表示8
小的数字(限于I, X, C)在大的数字的左边, 所表示的数等于大数减去小的数: 如IV 表示4
在一个数的上面画一条横线, 表示这个数增值1000倍(由于题目只考虑4000以内的数,所以这条规则不用考虑)。
不只是做题不会,看题也看不懂的一起举个手吧
"""
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
sum = 0
ROMAN = {'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1} for i in range(len(s)-1):
if (ROMAN[s[i]] < ROMAN[s[i+1]]):
sum = sum - ROMAN[s[i]]
else:
sum = sum + ROMAN[s[i]]
return sum + ROMAN[s[-1]]
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int I(1),V(5),X(10),L(50),C(100),D(500),M(1000) """ sum = 0 temp = 0 ROMAN = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} for ch in s: ch_val = int(ROMAN[ch]) sum = sum + ch_val if temp < ch_val: sum -= temp*2 temp = ch_val return sum
相关文章推荐
- LeetCode - 13. 罗马数字转整数
- Leetcode 13 Roman to Integer 罗马数字转整数
- Leetcode : 13 罗马数字转整数
- [Leetcode] Interger to roman 整数转成罗马数字
- 【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】
- leetcode-13-罗马数字转阿拉伯数字
- Leetcode简单题罗马数字转整数
- 【Leetcode-13】罗马数字转换为阿拉伯数字
- LeetCode | Integer to Roman(整数转换成罗马数字)
- leetCode 12.Integer to Roman (整数转罗马数字) 解题思路和方法
- leetcode 13 罗马数字转10进制
- (LeetCode) Roman to Integer --- 罗马数字转整数
- python解leetcode——13.Roman to Integer (罗马数字转为整数)
- leetcode 罗马数字与整数的转换算法
- LeetCode 13. Roman to Integer 罗马数字转整数
- LeetCode 12 Integer to Roman (整数转罗马数字)
- LeetCode习题笔记——罗马数字和整数之间的转换
- leetcode_13(罗马数字变成十进制)
- leetcode-java.T013_RomarToInteger 罗马数字转成整数数字
- leetcode:Integer to Roman(整数转化为罗马数字)