
๐ก ๋ฌธ์
๐ฏ ํ์ด ๊ณผ์
๋ก๋ง ์ซ์๋ฅผ ๋ณํํ๋ ๋ฌธ์ ์ ๋๋ค.
๋ก๋ง ์ซ์๋ ๊ฐ์ฐ, ๊ฐ์ฐ ์ฐ์ฐ๋ฒ์ ํจ๊ป I, V, X, L, C, D, M
์ ๋ฌธ์๋ฅผ ๋์ดํ์ฌ ํํํฉ๋๋ค.
๊ทธ๋ ๊ธฐ์ ์ ์๋ฆฌ๋ถํฐ ์ํํ๋ฉด์ ๊ทธ ๋ค์ ์ซ์์ ํฌ๊ธฐ๋ฅผ ๋น๊ตํ์ฌ ํฉ์ฐํด์ผํ ํฌ๊ธฐ๋ฅผ ์์๋ ๋๋ค.
์ด๋ ๋ฐ๋ณต๋ฌธ ๋์ ์ฌ๊ท ํธ์ถ์ ์ฌ์ฉํ๋ฉด ์ข ๋ ๊ฐ๊ฒฐํ๊ฒ ๊ตฌํํ ์ ์์ต๋๋ค.
๐จโ๐ป ์ฝ๋
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
const roman = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
function transform(here) {
if (here >= s.length) {
return 0
}
if (here + 1 < s.length && roman[s[here]] < roman[s[here + 1]]) {
return transform(here + 2) + roman[s[here + 1]] - roman[s[here]]
} else {
return transform(here + 1) + roman[s[here]]
}
}
return transform(0)
};
๋ฐ์ํ
'๐ algorithm > leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
LeetCode 1603 - Design Parking System (Easy) (0) | 2023.04.24 |
---|---|
LeetCode 15 - 3Sum (Medium) (0) | 2023.04.24 |
LeetCode 994 - Rotting Oranges (Medium) (0) | 2023.04.24 |
LeetCode 24 - Swap Nodes in Pairs (Medium) (0) | 2023.02.20 |
LeetCode 35 - Search Insert Position (Easy) (0) | 2023.02.20 |
๐ฌ ๋๊ธ