Leetcode - Roman to Integer (Python)

preview_player
Показать описание
February 2021 Leetcode Challenge
Leetcode - Roman to Integer #13
Difficulty: Easy (ish)
Рекомендации по теме
Комментарии
Автор

Thanks for always putting out videos to help people who don't understand. 👍

chengzhejiang
Автор

Really like your teaching style! Hope that you make more videos.

ahyeoncho
Автор

Hi Timothy, really like your approach and explanation - Simple and to the point, thanks!

GG-lebq
Автор

Hi Timothy, thanks so much for the explanation. The logic is so simple for something that appears so complicated! I just had one query; could you explain the i-=1 part, as I did not quite get that. Thanks again!

hari
Автор

Great! I guess Time and Space are constant?

VladBurlutsky
Автор

wonderful Timothy
Can you test the roman number "III" what will you get, Timothy?

tubex
Автор

what if the user types IVIV
you program is gonna print 8 but IVIV is not a number
u need to hundle some exceptions

abdelwahid_ab
Автор

Sir, I found an error of time limit exceeded.Time got run out and due to this it is not running on leetcode

surbhirohilla
Автор

god damn it, even if I draw block scheme and using pythontutor I can't realize this algorithm, especially i < N-1 and lookup[s[i]] < lookup[s[i]]

vladimirnamakonov
Автор

LOL! I for the life of me could not figure it out right to left. But left to to right was more intuitive for me.

letters = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
numbers = [1, 5, 10, 50, 100, 500, 1000]
mapp = {k:v for k, v in zip(letters, numbers)}

N = len(s)
value = 0
left = 0
while left < N:
if left + 1 < N and mapp[s[left]] < mapp[s[left+1]]:
value += mapp[s[left+1]] - mapp[s[left]]
left += 2
else:
value += mapp[s[left]]
left += 1
return value

janmichaelaustria
Автор

could you reiterate purpose of I < N-1 ?

sangpark
Автор

I haven't coded anything in a year because I switched schools and I have to finish some math courses before starting my first coding class even tho I know the language 🙄

FRAMEDSKATEKREW
Автор

I just started programming and this is my solution:

"
class Solution:
def romanToInt(self, s: str) -> int:

int_list = list()

for i in s:
if i == "I":
int_list.append(1)
elif i == "V":
int_list.append(5)
elif i == "X":
int_list.append(10)
elif i == "L":
int_list.append(50)
elif i == "C":
int_list.append(100)
elif i == "D":
int_list.append(500)
elif i == "M":
int_list.append(1000)

int_list.append(0)

sum = 0
last_num = 0

for i in int_list:

if last_num < i:
sum -= last_num

else:
sum += last_num

last_num = i

return sum

"

Extic