[LeetCode#12] Roman to Integer

Problem:

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

Analysis:

This problem is trivial, if you really understand the principle to construct a Roman number.
It is really really very very simple!
Note: Keep in mind!!! The character used in roman number include digit weight, but for numerial system it based on position. Thus you should find a way to convert the digit weight(in positional representation) into digital weight(through character combination)

for (int i = 0; i < 4; i++) {
    int digit_weight = (int)Math.pow(10, 3-i);
    int digit = num / digit_weight;
    switch (digit) {
        ...
    }
}

Solution:

public class Solution {
    public String intToRoman(int num) {
        if (num <= 0 || num > 3999)
            throw new IllegalArgumentException("The passed in argument is illegal");
        StringBuffer ret = new StringBuffer();
        HashMap<Integer, Character> map = new HashMap<Integer, Character> ();
        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');
        for (int i = 0; i < 4; i++) {
            int digit_weight = (int)Math.pow(10, 3-i);
            int digit = num / digit_weight;
            switch (digit) {
                case 1 : 
                    ret.append(map.get(digit_weight));
                    break;
                case 2 : 
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight));
                    break;
                case 3:
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight));
                    break;
                case 4:
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight * 5));
                    break;
                case 5:
                    ret.append(map.get(digit_weight * 5));
                    break;
                case 6:
                    ret.append(map.get(digit_weight * 5));
                    ret.append(map.get(digit_weight));
                    break;
                case 7:
                    ret.append(map.get(digit_weight * 5));
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight));
                    break;
                case 8:
                    ret.append(map.get(digit_weight * 5));
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight));
                    break;
                case 9:
                    ret.append(map.get(digit_weight));
                    ret.append(map.get(digit_weight * 10));
                    break;
            }
            num = num % digit_weight;
        }
        return ret.toString();
    }
}

 

posted @ 2015-09-02 11:33  airforce  阅读(125)  评论(0编辑  收藏  举报