Problems
Thinking
์์์ ์ ์ํ Symbol, Value๋ฅผ Dict๋ก ์ ์ธํด์ฃผ๊ณ mapping ์์ผ์ฃผ๋ฉด ๋๋ ๊ฐ๋จํ ๋ฌธ์ ๋ค.
์ ๊ฒฝ์จ์ผํ ๋ถ๋ถ์
์ด๋ ๊ฒ 2๊ฐ์ ๋ฌธ์๊ฐ ๋ง๋ฌ์ ๋ ์ซ์๊ฐ ๋ฐ๋๋ค๋ ์
ํ๋ํ๋ ๋ถ๊ธฐ์ฒ๋ฆฌํด์ ํ ๊น ์๊ฐํ๋๋ฐ
์คํ๋ ค ๋ ๋ณต์กํ๊ณ ์๊ฐ๋ ๋ง์ด ์์๋ ๊ฒ์ด๋ผ ํ๋จ
mapping ๋์ด์ผํ๋ dict์ ์ ์ธํด์ค๋ค
mapping = {
'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
}
input๋๋ Roman์ ํ๋์ฉ ์ชผ๊ฐ์ฃผ๊ณ ์์๋๋ก Interger๋ก ๋ฐ๊ฟ์ฃผ๋๋ฐ,
๊ทธ ๋ฐ๋ก ์์ ์๋ Roman๊ณผ ํจ๊ป ํ์ธํ๋๋ก ํด์ค๋ค.
now = 0
result = 0
num_list = list(s)
while now < len(num_list):
if (now+1 != len(num_list)) and (num_list[now] + num_list[now+1] in mapping):
result += mapping[num_list[now] + num_list[now+1]]
now += 2
else:
result += mapping[num_list[now]]
now += 1
return result
2๊ฐ Roman์ด mapping์ ์กด์ฌํ๋ค๋ฉด ํด๋นํ๋ integer๋ก ๋ฐ๊ฟ์ฃผ๊ณ ๋ค๋ค์ ์ซ์๋ก ๋์ด๊ฐ๋ค.
์๋ ๊ฒฝ์ฐ์ 1๊ฐ์ Roman์ integer๋ก ๋ฐ๊ฟ์ฃผ๋ ๋ค์ ์ซ์๋ก ๋์ด๊ฐ๋ค.
์ฑ~๊ณต
'๐ป leetcode > algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Algorithms | 21. Merge Two Sorted Lists | Python (1) | 2024.07.24 |
---|---|
[LeetCode] Algorithms | 20. Valid Parentheses | Python (0) | 2024.07.23 |
[LeetCode] Algorithms | 14. Longest Common Prefix | Python (0) | 2024.07.12 |