|
| 1 | +/** |
| 2 | +* Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. |
| 3 | +* |
| 4 | +* Symbol Value |
| 5 | +* I 1 |
| 6 | +* V 5 |
| 7 | +* X 10 |
| 8 | +* L 50 |
| 9 | +* C 100 |
| 10 | +* D 500 |
| 11 | +* M 1000 |
| 12 | +* |
| 13 | +* For example, two is written as II in Roman numeral, just two one's added |
| 14 | +* together. Twelve is written as, XII, which is simply X + II. The number |
| 15 | +* twenty seven is written as XXVII, which is XX + V + II. |
| 16 | +* |
| 17 | +* Roman numerals are usually written largest to smallest from left to right. |
| 18 | +* However, the numeral for four is not IIII. Instead, the number four is |
| 19 | +* written as IV. Because the one is before the five we subtract it making |
| 20 | +* four. The same principle applies to the number nine, which is written as IX. |
| 21 | +* |
| 22 | +* There are six instances where subtraction is used: |
| 23 | +* |
| 24 | +* I can be placed before V (5) and X (10) to make 4 and 9. |
| 25 | +* X can be placed before L (50) and C (100) to make 40 and 90. |
| 26 | +* C can be placed before D (500) and M (1000) to make 400 and 900. |
| 27 | +* |
| 28 | +* Given an integer, convert it to a roman numeral. Input is guaranteed to be |
| 29 | +* within the range from 1 to 3999. |
| 30 | +* |
| 31 | +* Example 1: |
| 32 | +* Input: 3 |
| 33 | +* Output: "III" |
| 34 | +* |
| 35 | +* Example 2: |
| 36 | +* Input: 4 |
| 37 | +* Output: "IV" |
| 38 | +* |
| 39 | +* Example 3: |
| 40 | +* Input: 9 |
| 41 | +* Output: "IX" |
| 42 | +* |
| 43 | +* Example 4: |
| 44 | +* Input: 58 |
| 45 | +* Output: "LVIII" |
| 46 | +* Explanation: L = 50, V = 5, III = 3. |
| 47 | +* |
| 48 | +* Example 5: |
| 49 | +* Input: 1994 |
| 50 | +* Output: "MCMXCIV" |
| 51 | +* Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. |
| 52 | +*/ |
| 53 | + |
| 54 | +public class IntegerToRoman12 { |
| 55 | +// 1, 5, 10, 50, 100, 500, 1000 |
| 56 | +private static char[] R = new char[]{'I', 'V', 'X', 'L', 'C', 'D', 'M'}; |
| 57 | +public String intToRoman(int num) { |
| 58 | +StringBuilder sb = new StringBuilder(); |
| 59 | +int base = 0; |
| 60 | +while (num > 0) { |
| 61 | +int digit = num % 10; |
| 62 | +if (digit > 0) { |
| 63 | +char[] cur = curr(digit, base); |
| 64 | +sb.insert(0, cur); |
| 65 | +} |
| 66 | +num /= 10; |
| 67 | +base += 2; |
| 68 | +} |
| 69 | +return sb.toString(); |
| 70 | +} |
| 71 | + |
| 72 | +private char[] curr(int digit, int base) { |
| 73 | +if (digit >= 1 && digit <= 3) { |
| 74 | +char[] res = new char[digit]; |
| 75 | +Arrays.fill(res, R[base]); |
| 76 | +return res; |
| 77 | +} else if (digit == 4) { |
| 78 | +return new char[]{R[base], R[base+1]}; |
| 79 | +} else if (digit == 5) { |
| 80 | +return new char[]{R[base+1]}; |
| 81 | +} else if (digit >= 6 && digit <= 8) { |
| 82 | +char[] res = new char[digit-5+1]; |
| 83 | +Arrays.fill(res, R[base]); |
| 84 | +res[0] = R[base+1]; |
| 85 | +return res; |
| 86 | +} else { // 9 |
| 87 | +return new char[]{R[base], R[base+2]}; |
| 88 | +} |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +/** |
| 93 | +* https://leetcode.com/problems/integer-to-roman/discuss/6274/Simple-Solution |
| 94 | +*/ |
| 95 | +public static String intToRoman2(int num) { |
| 96 | +String M[] = {"", "M", "MM", "MMM"}; |
| 97 | +String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; |
| 98 | +String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; |
| 99 | +String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; |
| 100 | +return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]; |
| 101 | +} |
| 102 | + |
| 103 | +} |
0 commit comments