-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path13_RomantoInteger.py
executable file
·40 lines (35 loc) · 1.1 KB
/
13_RomantoInteger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
symbols_integer = {"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
}
length = len(s)
integer = 0
isPass = False
for i in range(length):
# Subtractive notation use this symbol
if isPass:
isPass = False
continue
# Just add the integer
if s[i] in symbols_integer and s[i:i + 2] not in symbols_integer:
integer = integer + symbols_integer[s[i]]
isPass = False
continue
# Subtractive notation is used as follows.
if s[i:i + 2] in symbols_integer:
integer = integer + symbols_integer[s[i:i + 2]]
isPass = True
return integer
"""
"DCXXI"
"CDCM"
"""