-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ13_RomanToInteger
More file actions
48 lines (46 loc) · 1.41 KB
/
Q13_RomanToInteger
File metadata and controls
48 lines (46 loc) · 1.41 KB
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
41
42
43
44
45
46
47
48
class Solution:#first test 204ms
def romanToInt(self, s):
dic={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
num=[]
val=dic[s[0]]
num.append(dic[s[0]])
if len(s)>1:
for i in range(1,len(s)):
num.append(dic[s[i]])
if num[i]<=num[i-1]:
val=val+num[i]
else:
val=val+num[i]-2*num[i-1]
return val
else:
return val
class Solution:#second test 196ms
def romanToInt(self, s):
dic={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
if len(s)==1 :
return dic[s]
else:
val=dic[s[0]]
for i in range(len(s)-1):
num1 = dic[s[i]]
num2 = dic[s[i+1]]
if num1 < num2:
val += num2 - 2*num1
else:
val += num2
return val
class Solution:#third test 196ms
def romanToInt(self, s):
dic={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
if len(s)==1 :
return dic[s]
else:
val=0
for i in range(len(s)-1):
num1 = dic[s[i]]
num2 = dic[s[i+1]]
if num1 < num2:
val -= num1
else:
val += num1
return val + dic[s[-1]]