본문 바로가기
LeetCode

[LeetCode] 12. Integer to Roman

by JungSeung 2023. 10. 19.
728x90

https://leetcode.com/

Roman numerals are represented by seven different symbols: IVXLCD and M.

 

Symbol       Value
I                   1
V                  5
X                 10
L                  50
C                 100
D                 500
M                1000

 

For example, 2 is written as II in Roman numeral, just two one's added together. 

12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII

Instead, the number four is written as IV. Because the one is before the five we subtract it making four. 

The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X  can be placed before L (50) and C (100) to make 40 and 90.
  • C  can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.

Example 1:

Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.

 

Example 2:

Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

 

Example 3:

Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

Constraints:

  • 1 <= num <= 3999

 

Code :

/*
	1. value 객체에는 로마 숫자와 해당하는 정수 값이 포함되어 있습니다.
	2. 주어진 정수를 로마 숫자로 변환하는 함수 intToRoman은 반복문을 사용하여 주어진 정수를 로마 숫자로 변환합니다.
	3. 주어진 정수를 로마 숫자로 변환하기 위해 value 객체를 이용하여 몫과 나머지를 계산합니다.
	4. 각 로마 숫자의 등장 횟수는 몫에 따라 결정됩니다.
	5. 변환된 로마 숫자들을 result에 누적하여 최종적으로 변환된 로마 숫자를 반환합니다.
*/
const value = {
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1
};

var intToRoman = function(num) {
    let result = '';
    for (let s of Object.keys(value)) {
        const r = Math.floor(num / value[s]);
        num -= r * value[s];
        result += s.repeat(r);
    }
    return result;
};

 

Solutions Code :

/*
	1. ones, tens, hrns, ths 배열은 각 자리수에 대한 로마 숫자들을 포함합니다.
	2. intToRoman 함수는 주어진 정수를 1000, 100, 10, 1의 자리로 분리하여 각 자리에 맞는 로마 숫자를 찾아서 결합합니다.
	3. Math.floor 함수를 사용하여 정수 나눗셈의 몫을 구하고, 해당하는 로마 숫자를 배열에서 가져와 문자열에 덧붙입니다.
	4. 마지막으로 각 자리수의 로마 숫자를 조합하여 최종적으로 변환된 로마 숫자를 반환합니다.
*/
var intToRoman = function(num) {
    const ones = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
    const tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
    const hrns = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
    const ths = ["", "M", "MM", "MMM"];
    return ths[Math.floor(num / 1000)] + hrns[Math.floor((num % 1000) / 100)] + tens[Math.floor((num % 100) / 10)] + ones[num % 10];
};

 

출처 : https://leetcode.com/problemset/all/

 

Problems - LeetCode

Boost your coding interview skills and confidence by practicing real interview questions with LeetCode. Our platform offers a range of essential problems for practice, as well as the latest questions being asked by top-tier companies.

leetcode.com

 

728x90

'LeetCode' 카테고리의 다른 글

[LeetCode] 14. Longest Common Prefix  (0) 2023.10.20
[LeetCode] 13. Roman to Integer  (2) 2023.10.19
[LeetCode] 11. Container With Most Water  (0) 2023.10.18
[LeetCode] 10. Regular Expression Matching  (0) 2023.10.18
[LeetCode] 9. Palindrome Number  (0) 2023.10.17