[Leetcode] Integer to Roman

lkimilhol·2021년 2월 28일
0

코딩테스트

목록 보기
3/8

https://leetcode.com/problems/integer-to-roman/

Given an integer, convert it to a roman numeral.


정수가 주어지면 로마 숫자로 표기 하라는 내용의 문제입니다.

문제 내용을 조금 더 추가하자면

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

아시다시피 숫자 2는 로마숫자 II로 표기가 가능합니다.
또한 12는 XII로 표기가 됩니다. 이처럼 정수 num을 로마숫자로 표기해 주는것이 이번 문제입니다.

저는 문제의 힌트로 주어진 부분을 고려 하여 문제를 풀었는데요. 힌트는 아래와 같습니다.

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.

I는 V와 X앞에만 올 수 있고, X는 L과 C 앞에만 올 수 있는 것이죠.


4, 9
40, 90
400, 900 을 표기하는 방법만 조금 고려 해 주면 된다고 생각했습니다.

소스 코드 또한 if문을 풀어놓게 되었는데요. 다른 깔끔한 방법은 있을거라 생각합니다. 소스 코드는 아래와 같습니다.

public class IntegerToRoman {
    private static final int I = 1;
    private static final int V = 5;
    private static final int X = 10;
    private static final int L = 50;
    private static final int C = 100;
    private static final int D = 500;
    private static final int M = 1000;

    public static String solution(int num) {
        int total = 0;
        int calc = num;
        StringBuilder ret = new StringBuilder();

        while (total != num) {
            if (calc / M > 0 ) {
                total += M;
                calc -= M;
                ret.append("M");
            }
            else if (calc / D > 0) {
                if (calc >= 900) {
                    total += 900;
                    calc -= 900;
                    ret.append("CM");
                    continue;
                }
                total += 500;
                calc -= 500;
                ret.append("D");
            } else if (calc / C > 0) {
                if (calc >= 400) {
                    total += 400;
                    calc -= 400;
                    ret.append("CD");
                    continue;
                }
                total += 100;
                calc -= 100;
                ret.append("C");
            } else if (calc / L > 0) {
                if (calc >= 90) {
                    total += 90;
                    calc -= 90;
                    ret.append("XC");
                    continue;
                }
                total += 50;
                calc -= 50;
                ret.append("L");
            } else if (calc / X > 0) {
                if (calc >= 40) {
                    total += 40;
                    calc -= 40;
                    ret.append("XL");
                    continue;
                }
                total += 10;
                calc -= 10;
                ret.append("X");
            } else if (calc / V > 0) {
                if (calc >= 9) {
                    total += 9;
                    calc -= 9;
                    ret.append("IX");
                    continue;
                }
                total += 5;
                calc -= 5;
                ret.append("V");
            } else if (calc / I > 0) {
                if (calc >= 4) {
                    total += 4;
                    calc -= 4;
                    ret.append("IV");
                    continue;
                }
                total += 1;
                calc -= 1;
                ret.append("I");
            }
        }

        return ret.toString();
    }
}

별 다른 내용없이 위의 설명과 마찬가지로 4, 9나 40, 90을 고려하였습니다. 그리고 int calc를 선언하여 계산 해야 하는 값을 유동적으로 업데이트 해주면서 풀이하였습니다.

별로 어렵지 않은 문제였습니다. 시간은 20분 정도 걸린거 같네요.

앞으로는 dfs나 dp같은 문제를 공부해야겠습니다.

profile
백엔드 개발자

0개의 댓글