[leetcode] Medium-12번. Integer to Roman 문제풀이

[leetcode] Medium-12번. Integer to Roman 문제풀이

728x90

문제

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

풀이코드

package SITE03_leetcode.medium; import java.util.*; import java.util.stream.Collectors; /** * https://leetcode.com/problems/integer-to-roman/ */ public class M008_leetCode12_Integer_to_Roman { public static TreeMap map = new TreeMap<>(); static { map.put(1,"I"); map.put(5, "V"); map.put(10, "X"); map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M"); map.put(4, "IV"); map.put(9, "IX"); map.put(40, "XL"); map.put(90, "XC"); map.put(400, "CD"); map.put(900, "CM"); } public static void main(String[] args) { M008_leetCode12_Integer_to_Roman solution = new M008_leetCode12_Integer_to_Roman(); System.out.println(solution.intToRoman(20)); } public String intToRoman(int num) { StringBuilder result = new StringBuilder(); while (num != 0) { // 제공된 키 값보다 같거나 작은 값 중 가장 큰 키의 Entry 를 반환 Map.Entry entry = map.floorEntry(num); result.append(entry.getValue()); num -= entry.getKey(); } return result.toString(); } }

TreeMap

위 풀이코드를 이해하기 위해서 정리한 TreeMap 을 공부하자.

https://devfunny.tistory.com/577

from http://devfunny.tistory.com/578 by ccl(A) rewrite - 2021-11-02 01:04:42