@@ -0,0 +1,2790 @@
+package leetcode;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.Stack;%0Aimport java.util.TreeSet;%0A%0A/**%0A * https://leetcode.com/problems/integer-to-roman/%0A */%0Apublic class Problem12 %7B%0A    public String intToRoman(int num) %7B%0A        Map%3CInteger, String%3E map = buildRomanNumerals();%0A        Set%3CInteger%3E keys = new TreeSet%3C%3E(map.keySet());%0A        int n = 0;%0A        int tmp = num;%0A        while (tmp != 0) %7B%0A            tmp /= 10;%0A            n++;%0A        %7D%0A        Stack%3CInteger%3E numbers = new Stack%3C%3E();%0A        tmp = num;%0A        int b = 0;%0A        for (int i = 1; i %3C= n; i++) %7B%0A            int a = tmp %25 (int) (Math.pow(10, i));%0A            numbers.push(a - b);%0A            b = a;%0A        %7D%0A        StringBuilder roman = new StringBuilder();%0A        while (!numbers.isEmpty()) %7B%0A            int number = numbers.pop();%0A            if (map.containsKey(number)) %7B%0A                roman.append(map.get(number));%0A            %7D else %7B%0A                if (number == 0) %7B%0A                    continue;%0A                %7D%0A                Roman r = find(number, map, keys);%0A                int y = number / r.integer;%0A                for (int i = 0; i %3C y; i++) %7B%0A                    roman.append(r.roman);%0A                %7D%0A                int x = number %25 r.integer;%0A                while (x != 0) %7B%0A                    r = find(x, map, keys);%0A                    y = x / r.integer;%0A                    for (int i = 0; i %3C y; i++) %7B%0A                        roman.append(r.roman);%0A                    %7D%0A                    x = x %25 r.integer;%0A                    %0A                %7D%0A            %7D%0A        %7D%0A        return roman.toString();%0A    %7D%0A    %0A    private static class Roman %7B%0A        private Integer integer;%0A        private String roman;%0A        %0A        public Roman(Integer integer, String roman) %7B%0A            this.integer = integer;%0A            this.roman = roman;%0A        %7D%0A    %7D%0A    %0A    private Roman find(int number, Map%3CInteger, String%3E map, Set%3CInteger%3E keys) %7B%0A        Integer integer = null;%0A        String roman = null;%0A        for (int key : keys) %7B%0A            if (number %3C key) %7B%0A                break;%0A            %7D%0A            integer = key;%0A            roman = map.get(key);%0A        %7D%0A        return new Roman(integer, roman);%0A    %7D%0A    %0A    private Map%3CInteger, String%3E buildRomanNumerals() %7B%0A        Map%3CInteger, String%3E map = new HashMap%3C%3E();%0A        map.put(1, %22I%22);%0A        map.put(4, %22IV%22);%0A        map.put(5, %22V%22);%0A        map.put(9, %22IX%22);%0A        map.put(10, %22X%22);%0A        map.put(40, %22XL%22);%0A        map.put(50, %22L%22);%0A        map.put(90, %22XC%22);%0A        map.put(100, %22C%22);%0A        map.put(400, %22CD%22);%0A        map.put(500, %22D%22);%0A        map.put(900, %22CM%22);%0A        map.put(1000, %22M%22);%0A        %0A        return map;%0A    %7D%0A%7D%0A

Solve problem 12