Just show how I think about this problem.


  • 0

    This is not beautiful code. But the thinking process is pretty clear. You can also make the code below more concise very easily.

    If you know how the Roman Numeral works, it is very easy to deal with this question. Here are two good websites:

    http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

    http://number.webmasters.sk/roman.php

    public String intToRoman(int num) {
            // I1 V5 X10 L50 C100 D500 M1000
            // 左一(4),右三(1-3),整(0,5)
            int count = 0;
            ArrayList<String> temp = new ArrayList();
            ArrayList<String> res = new ArrayList();
                // M1000
                count = num / 1000;
                num = num % 1000;
                for (int i = 0; i < count; i++) {
                    res.add("M");
                }
                // D500
                /// 900 - 1000
                if (num >= 900) {
                    num = num - 900;
                    res.add("CM");
                } else {
                    /// 500 - 900
                    if (num >= 500) {
                        res.add("D");
                        num = num - 500;
                    }
                }
                // C100
                count = num / 100;
                num = num % 100;
                if (count == 4) {
                    res.add("CD");
                } else {
                    for (int i = 0; i < count; i++) {
                        res.add("C");
                    }
                }
                // L50
                if (num >= 90) {
                    num = num - 90;
                    res.add("XC");
                } else {
                    /// 50 - 90
                    if (num >= 50) {
                        res.add("L");
                        num = num - 50;
                    }
                }
                // X10
                count = num / 10;
                num = num % 10;
                if (count == 4) {
                    res.add("XL");
                } else {
                    for (int i = 0; i < count; i++) {
                        res.add("X");
                    }
                }
                // V5
                if (num >= 9) {
                    num = num - 9;
                    res.add("IX");
                } else {
                    /// 5 - 9
                    if (num >= 5) {
                        res.add("V");
                        num = num - 5;
                    }
                }
                // I1
                count = num;
                num = num % 1;
                if (count == 4) {
                    res.add("IV");
                } else {
                    for (int i = 0; i < count; i++) {
                        res.add("I");
                    }
                }
            String result = res.get(0);
            for (int i = 1; i < res.size(); i++) {
                result += res.get(i);
            }
            return result;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.