Skip to content

Update 001_Two_Sum.py #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update 013_Roman_To_Integer.py
  • Loading branch information
backendbuilderdev authored Aug 18, 2023
commit 8d8a0fd3d31755e342a37a50c6d87399b9292ec1
56 changes: 41 additions & 15 deletions 013_Roman_To_Integer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
#convert Roman to Integer
class Solution:
# @param {string} s
# @return {integer}
def romanToInt(self, s):
roman = {'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1}

result = 0
for i in range(len(s)-1):
if roman[s[i]]<roman[s[i+1]]:
result-=roman[s[i]]
def romanToInt(self, s: str) -> int:
roman = {
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000
}
s=s.replace('IV', 'IIII').replace('IX','IIIIIIIII')
s=s.replace('XL', 'XXXX').replace('XC','XXXXXXXXX')
s=s.replace('CD', 'CCCC').replace('CM','CCCCCCCCC')

L = []
temp = s[0]
for i in range(1,len(s)):
if s[i] == s[i-1]:
temp += s[i]
else:
result+=roman[s[i]]
else:
result+=roman[s[-1]]

return result
L.append(temp)
temp = s[i]
if i == len(s)-1:
L.append(temp)
sum = 0
for i in L:
print(i)
if i[0] == 'M':
sum += roman['M'] * len(i)
elif i[0] =='C':
sum += roman['C'] * len(i)
elif i[0] =='D':
sum += roman['D'] * len(i)
elif i[0] =='L':
sum += roman['L'] * len(i)
elif i[0] =='X':
sum += roman['X'] * len(i)
elif i[0] =='V':
sum += roman['V'] * len(i)
elif i[0] =='I':
sum += roman['I'] * len(i)
return sum