文章

LeetCode Integer To Roman

Problem

Given an integer, convert it to a roman numeral.

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

Roman To Integer是对应问题。即将整数转换成对应的罗马数字。罗马数字规则见wiki:罗马数字规则

Java 实现(个人解法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.coderli.leetcode.algorithms.medium;

/**
 * Given an integer, convert it to a roman numeral.
 * <p>
 * Input is guaranteed to be within the range from 1 to 3999
 *
 * @author li.hzh
 */
public class IntegerToRoman {

    public static void main(String[] args) {
        IntegerToRoman integerToRoman = new IntegerToRoman();
        System.out.println(integerToRoman.intToRoman(1));
        System.out.println(integerToRoman.intToRoman(10));
        System.out.println(integerToRoman.intToRoman(90));
        System.out.println(integerToRoman.intToRoman(1437));
    }

    public String intToRoman(int num) {
        int times = 1;
        String result = "";
        while (num > 0) {
            int tempIntValue = num % 10;
            num /= 10;
            String tempStrValue = "";
            if (tempIntValue == 5) {
                switch (times) {
                    case 1:
                        result = tempStrValue = "V";
                        break;
                    case 2:
                        result = "L" + result;
                        break;
                    case 3:
                        result = "D" + result;
                        break;
                }
            } else if (tempIntValue % 5 <= 3) {
                int length = tempIntValue;
                switch (times) {
                    case 1:
                        if (tempIntValue > 5) {
                            tempStrValue = "V";
                            length = tempIntValue - 5;
                        }
                        for (int i = 0; i < length; i++) {
                            tempStrValue += "I";
                        }
                        result = tempStrValue + result;
                        break;
                    case 2:
                        if (tempIntValue > 5) {
                            tempStrValue = "L";
                            length = tempIntValue - 5;
                        }
                        for (int i = 0; i < length; i++) {
                            tempStrValue += "X";
                        }
                        result = tempStrValue + result;
                        break;
                    case 3:
                        if (tempIntValue > 5) {
                            tempStrValue = "D";
                            length = tempIntValue - 5;
                        }
                        for (int i = 0; i < length; i++) {
                            tempStrValue += "C";
                        }
                        result = tempStrValue + result;
                        break;
                    case 4:
                        for (int i = 0; i < tempIntValue; i++) {
                            tempStrValue += "M";
                        }
                        result = tempStrValue + result;
                        break;
                }
            } else {
                switch (times) {
                    case 1:
                        result = tempIntValue < 5 ? "IV" + result : "IX" + result;
                        break;
                    case 2:
                        result = tempIntValue < 5 ? "XL" + result : "XC" + result;
                        break;
                    case 3:
                        result = tempIntValue < 5 ? "CD" + result : "CM" + result;
                        break;
                }
            }
            times++;
        }
        return result;
    }

}


分析

上面解法其实受到了罗马转整数问题的惯性思维。把思维限制到了罗马数字只能用基础的I、V、X等元素上。罗马数字的规则不再介绍了。思路其实跟罗马转数字一致,抓住罗马数字其实也是个位、十位、百位等一位一位组合起来的,因此我们一位一位的生成对应的罗马数字即可。

正因为罗马数字也是一位一位对应的,因此网上流传一个简便的写法,代码很简洁:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static String change(int num) {
		String [][]str = 
			{
		            {"","I","II","III","IV","V","VI","VII","VIII","IX"},
		            {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
		            {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
		            {"","M","MM","MMM"}
			};
		String roman = (str[3][num / 1000 % 10])+(str[2][num / 100 % 10])+(str[1][num / 10 % 10])+(str[0][num % 10]);
		
		return roman;


把个十百千各位可能罗马字符准备好,然后直接计算整数对应位上的值,取出数组中对应索引位的罗马值拼到一起就可以了。

本文由作者按照 CC BY 4.0 进行授权