Created
July 28, 2018 10:38
-
-
Save luojiyin1987/5b087bd550e61d484290ea595c0abfd7 to your computer and use it in GitHub Desktop.
Roman to Integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def romanToInt(self, s): | |
| """ | |
| :type s: str | |
| :rtype: int | |
| """ | |
| sum = 0 | |
| last = None | |
| numbers = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1} | |
| for st in s: | |
| if last and numbers[st] >last: | |
| sum -= 2 * last | |
| sum += numbers[st] | |
| last = numbers[st] | |
| return sum | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment