【零基础刷Leetcode】13. Roman to Integer Python
2018-03-16 02:45
267 查看
13. Roman to Integer Python
边刷边记录,因为没有基础,所以很慢,总觉得自己可以写出来才证明自己懂了这题,有写的不对的地方,欢迎指正。转载请标明出处哦。03/15/2018
原题链接:https://leetcode.com/problems/roman-to-integer/description/
翻译
给定一个罗马数字,将其转成对应的整数。输入限制在1到3999之间。思路
Leetcode上给了两个hints
Hint 1: 罗马数字与整数的对应表,我直接用dictionary表示了Roman = { I:1, V:5, X:10, L:50, C:100, D:500, M:1000}Hint 2: 转换的规则
如果I(1)在 V(5) 或者 X(10) 之前, 减去1,例如:IV = 5-1=4, IX = 10-1=9也就是 I<V,I<X时,如果 I在 V或者X之前,就要减去I的值。反之如果I在V或X之后,则加上I的值, 例如:VI = 5+1 = 6
如果X(10)在 L(50) 或者 C(100) 之前, 减去10,例如:CD = 50-10=40, CM = 100-10=90 也就是 X<L,X<C时,如果 X在 L或者C之前,就要减去X的值。反之如果X在L或C之后,则加上X的值, 例如:XL = 50+10 = 60
如果C(100)在 D(500) 或者 M(1000) 之前, 减去10,例如:CD = 500-100=400, CM = 1000-100=900 也就是 C<D,C<M 时,如果 C在 D或者M之前,就要减去C的值。反之如果C在D或M之后,则加上C的值, 例如:DC = 500+100 = 600
相关文章推荐
- Leetcode 13. Roman to Integer(python)
- python写算法题:leetcode: 13. Roman to Integer
- 13. Roman to Integer leetcode Python 2016 new Season
- LeetCode 13. Roman to Integer
- [LeetCode] 13. Roman to Integer 罗马数字转为整数
- LeetCode --- 13. Roman to Integer
- [LeetCode]题解(python):008-String to Integer (atoi)
- Reverse Integer Leetcode Python Java
- [勇者闯LeetCode] 13. Roman to Integer
- leetCode 13. Roman to Integer 字符串
- LeetCode 13. Roman to Integer
- [leetcode] 13. Roman to Integer
- 【LeetCode】13. Roman to Integer (2 solutions)
- 【Leetcode】13. Roman to Integer
- [leetcode] Roman to Integer @ Python
- [leetcode] 13. Roman to Integer[facebook](11)
- 12. Integer to Roman Leetcode Python
- LeetCode 8 — String to Integer (atoi)(C++ Java Python)
- leetcode:13. Roman to Integer
- Leetcode 12. Integer to Roman(python)