知之者 不如好之者, 好之者 不如樂之者

기계처럼 살지 말고, 즐기는 인간이 되자

Code/LeetCode

[LeetCode] 13. Roman to Integer (Easy/Python)

코방코 2022. 8. 31. 12:11
728x90

From today, I will write my code post in English.

There may be grammatical or spelling errors.

 

Roman to Integer - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

This is the link to problem 13.

 

Explain the problem easily, just change the roman number to an integer.

By using I=1, V=5, X=10, L=50, C=100, D=500, M=1000

But there is one thing to care about.

There can't be four consecutive identical roman numerals.

So they express 4 written as IV.

If a small number is placed before a large number, then subtract a small number from a large number.

 

Below is the solution I wrote.

Add roman to the result when it is larger than the next roman number.

Subtract roman from the result when it is smaller than the next roman number.

 

The time efficiency of this code is in the top 11.60%, and the space efficiency is in the top 69.68%.

 

 

728x90
반응형