π‘ λ¬Έμ
π― νμ΄ κ³Όμ
λ‘λ§ μ«μλ₯Ό λ³ννλ λ¬Έμ μ λλ€.
λ‘λ§ μ«μλ κ°μ°, κ°μ° μ°μ°λ²μ ν¨κ» 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 |
π¬ λκΈ