이것저것 해보기🌼

[leetcode] 12. Integer to Roman 본문

코딩테스트

[leetcode] 12. Integer to Roman

realtree 2021. 6. 27. 13:34

문제 )

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

 

 

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"

 

Example 2:

Input: num = 4 Output: "IV"

 

Example 3:

Input: num = 9 Output: "IX"

 

Example 4:

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

 

Example 5:

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

 

Constraints:

  • 1 <= num <= 3999

 

입력 숫자도 3천대까지 이기 때문에 손쉽게 구현할 수 있었다.

 

나와있는 기준인 1000,500,100,50,10,5,1 단위로 나누고 나머지 값에 대해 다시 연산을 하도록 반복문을 만들었다.

그리고 예외케이스인 4와 9가 들어가는 숫자에 대해서만 각각 케이스를 나눠주었다.

단순 구현으로 푼다면 크게 어렵지 않은 문제였다.

 

 

class Solution {
    public String intToRoman(int num) {
        String answer = "";
        
        while(num > 0){
            if(num/1000>=1)
            {
                for(int i=0; i< num/1000 ; i++){
                    answer += "M";
                }
                num = num%1000;
            }else if(num/500 >= 1){
                if((num%1000)/100 == 9){
                    answer += "CM";
                    num -= 900;
                }
                else{
                    answer += "D";
                    num -= 500;
                }
            
            }else if(num/100 >= 1){
                if((num%500)/100 == 4){
                    answer += "CD";
                    num -= 400;
                }
                else{
                    for(int i=0; i< (num%500)/100 ; i++){
                        answer += "C";
                    }
                    num = num%100;
                }
            }else if(num/50 >= 1){
                if((num%100)/10 == 9){
                    answer += "XC";
                    num -= 90;
                }
                else {
                    answer += "L";
                    num = num - 50;
                }
            }else if(num/10 >= 1){
                if((num%50)/10 == 4){
                    answer+= "XL";
                }
                else{
                    for(int i=0; i< (num%50)/10 ; i++){
                        answer += "X";
                    }
                }
                num = num%10;
            }else if(num/5 >= 1){
                if(num%10 == 9){
                    answer += "IX";
                }
                else{
                    answer += "V";
                    for(int i=0; i< num%10 - 5 ; i++){
                        answer += "I";
                    }
                }
                num = 0;
            }else {
                if(num%5 == 4){
                    answer += "IV";
                }else {
                    for(int i=0; i< num%5 ; i++){
                        answer += "I";
                    }
                }
                num = 0;
            }
        }
        
        return answer;
    }
}

 

 

문제링크 : https://leetcode.com/problems/integer-to-roman/ 

 

Integer to Roman - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com