[LintCode] Integer to Roman & Roman to Integer

322 查看

Problem

Integer to Roman
Given an integer, convert it to a roman numeral.
The number is guaranteed to be within the range from 1 to 3999.

Roman to Integer
Given a roman numeral, convert it to an integer.
The answer is guaranteed to be within the range from 1 to 3999.

Symbol   Value
I        1
V        5
X        10
L        50
C        100
D        500
M        1,000

Note

Integer to Roman:

建立映射:整数数组num -- 字符串数组roman,这两个数组都要从大到小,为了方便之后对整数n进行从大到小的分解,以便用StringBuilder()从前向后建立Roman数字。

Roman to Integer:

建立HashMap,存入Roman的数值对应关系。然后从String s从前向后遍历每个字符,找到map对应的值累加,if遇到前一位值小于后一位值的情况,减去前一位值的2倍(if外面多加了一次,减2倍减回来)。

Solution

Integer to Roman

public class Solution {
    public String intToRoman(int n) {
        // Write your code here
        String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int[] num = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (n != 0) {
            if (n >= num[i]) {
                sb.append(roman[i]);
                n -= num[i];
            }
            else i++;
        }
        return sb.toString();
    }
}

Roman to Integer

public class Solution {
    public int romanToInt(String s) {
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        map.put('M', 1000);
        map.put('D', 500);
        map.put('C', 100);
        map.put('L', 50);
        map.put('X', 10);
        map.put('V', 5);
        map.put('I', 1);
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            res += map.get(s.charAt(i));
            if (i > 0 && map.get(s.charAt(i-1)) < map.get(s.charAt(i))) 
                res -= 2 * map.get(s.charAt(i-1));
        }
        return res;
    }
}