Leetcode: Integer to Roman
18617 ワード
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
一度に
1 public class Solution {
2 public String intToRoman(int num) {
3 StringBuffer res = new StringBuffer();
4 if (num > 3999 || num < 1) return res.toString();
5 int copy = num;
6 int thousand = copy / 1000; //# of thousand
7 copy = copy - thousand * 1000;
8 int hundred = copy / 100; //# of hundred
9 copy = copy - hundred * 100;
10 int ten = copy / 10; //# of ten
11 copy = copy - ten * 10;
12 int one = copy / 1; //# of one
13
14 if (thousand != 0) { //thousand
15 switch (thousand) {
16 case 1: res.append("M"); break;
17 case 2: res.append("MM"); break;
18 case 3: res.append("MMM"); break;
19 }
20 }
21
22 if (hundred != 0) { //thousand
23 switch (hundred) {
24 case 1: res.append("C"); break;
25 case 2: res.append("CC"); break;
26 case 3: res.append("CCC"); break;
27 case 4: res.append("CD"); break;
28 case 5: res.append("D"); break;
29 case 6: res.append("DC"); break;
30 case 7: res.append("DCC"); break;
31 case 8: res.append("DCCC"); break;
32 case 9: res.append("CM"); break;
33 }
34 }
35
36 if (ten != 0) { //thousand
37 switch (ten) {
38 case 1: res.append("X"); break;
39 case 2: res.append("XX"); break;
40 case 3: res.append("XXX"); break;
41 case 4: res.append("XL"); break;
42 case 5: res.append("L"); break;
43 case 6: res.append("LX"); break;
44 case 7: res.append("LXX"); break;
45 case 8: res.append("LXXX"); break;
46 case 9: res.append("XC"); break;
47 }
48 }
49
50 if (one != 0) { //thousand
51 switch (one) {
52 case 1: res.append("I"); break;
53 case 2: res.append("II"); break;
54 case 3: res.append("III"); break;
55 case 4: res.append("IV"); break;
56 case 5: res.append("V"); break;
57 case 6: res.append("VI"); break;
58 case 7: res.append("VII"); break;
59 case 8: res.append("VIII"); break;
60 case 9: res.append("IX"); break;
61 }
62 }
63 return res.toString();
64 }
65 }
このようにするのは巧みな疑いがあるに違いない.それとも、Roman Numeralsの方法で正直に構築すべきか.Code Gankerは良い方法を提供した.
1 public String intToRoman(int num) {
2 //I 1
3 //V 5
4 //X 10
5 //L 50
6 //C 100
7 //D 500
8 //M 1,000
9 if(num<1 || num>3999)
10 return "";
11 int digit = 1000;
12 ArrayList<Integer> digits = new ArrayList<Integer>();
13 while(digit>0)
14 {
15 digits.add(num/digit);
16 num %= digit;
17 digit /= 10;
18 }
19 StringBuilder res = new StringBuilder();
20 res.append(convert(digits.get(0),'M',' ', ' '));
21 res.append(convert(digits.get(1),'C','D', 'M'));
22 res.append(convert(digits.get(2),'X','L', 'C'));
23 res.append(convert(digits.get(3),'I','V', 'X'));
24 return res.toString();
25 }
26 public String convert(int digit, char one, char five, char ten)
27 {
28 StringBuilder res = new StringBuilder();
29 switch(digit)
30 {
31 case 9:
32 res.append(one);
33 res.append(ten);
34 break;
35 case 8:
36 case 7:
37 case 6:
38 case 5:
39 res.append(five);
40 for(int i=5;i<digit;i++)
41 res.append(one);
42 break;
43 case 4:
44 res.append(one);
45 res.append(five);
46 break;
47 case 3:
48 case 2:
49 case 1:
50 for(int i=0;i<digit;i++)
51 res.append(one);
52 break;
53 default:
54 break;
55 }
56 return res.toString();
57 }