넘치게 채우기

[LeetCode] 12. Integer to Roman 본문

PS/LeetCode

[LeetCode] 12. Integer to Roman

riveroverflow 2023. 8. 1. 16:28
728x90
반응형

https://leetcode.com/problems/integer-to-roman/description/?envType=study-plan-v2&envId=top-interview-150 

 

Integer to Roman - LeetCode

Can you solve this real interview question? Integer to Roman - Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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 tw

leetcode.com

문제 유형 : 그리디

문제 난이도 : Medium

 

문제

Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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.

 

Constrains:

1 <= num <= 3999

 

주어지는 정수를 로마자로 바꿔라.

정수는 1부터 3999까지의 범위를 가진다.

 

풀이

1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1순으로 최대한 뺄 수 있을 때까지 수를 빼고 문자를 추가하면 된다.

 

거스름돈 문제에서 경우의 수가 조금 더 늘어난 버전이라고 볼 수 있다.

 

코드(C++)

class Solution {
public:
    string intToRoman(int num) {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);

        int n = num;
        string roman;

        while(n >= 1000){
            n -= 1000;
            roman.push_back('M');
        }
        while(n >= 900){
            n -= 900;
            roman += "CM";
        }
        while(n >= 500){
            n -= 500;
            roman.push_back('D');
        }
        while(n >= 400){
            n -= 400;
            roman += "CD";
        }
        while(n >= 100){
            n -= 100;
            roman.push_back('C');
        }
        while(n >= 90){
            n -= 90;
            roman += "XC";
        }
        while(n >= 50){
            n -= 50;
            roman.push_back('L');
        }
        while(n >= 40){
            n -= 40;
            roman += "XL";
        }
        while(n >= 10){
            n -= 10;
            roman.push_back('X');
        }
        while(n >= 9){
            n -= 9;
            roman += "IX";
        }
        while(n >= 5){
            n -= 5;
            roman.push_back('V');
        }
        while(n >= 4){
            n -= 4;
            roman += "IV";
        }
        while(n >= 1){
            n -= 1;
            roman.push_back('I');
        }

        return roman;
    }
};
728x90
반응형

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] 151. Reverse Words in a String  (0) 2023.08.03
[LeetCode] 14. Longest Common Prefix  (0) 2023.08.02
[LeetCode] 42. Trapping Rain Water  (0) 2023.07.31
[LeetCode] 135. Candy  (0) 2023.07.30
[LeetCode] 238. Product of Array Except Self  (0) 2023.07.26