This is Roman numerals converted to decimal numerals Postorder
Title Requirements: Converting decimal digits into Roman digits, the numerical range is [1,3999]
stay Conversion of Roman numerals into decimal numbers In my blog, I briefly introduced how to convert Roman numerals into decimal numerals. In this topic, we need to further understand the protocol for converting decimal numbers into Roman numbers.
There are seven individual figures in Rome, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000).
The values that can be subtracted are I (1), X (10), C (100), and each value can be subtracted at most once.
Left subtraction should not be cross-digit. For example, 99 cannot be represented as IC, but XCIX.
Multiple identical letters are added when they appear in succession, but letters cannot be repeated more than four times.
To sum up, the number of bits can be expressed by I, V and X. Similarly, the tree of ten bits can be represented by X, L and C, the tree of one hundred bits can be represented by C, D and M, and the number of thousand bits can be expressed by M (the number is no more than 3999).
So we can calculate the values on each bit of the integer, get the corresponding Roman numerals, and finally merge them into the final Roman numerals.
Idea 1: Traverse the values of each bit and convert them into Roman numerals
I tried two methods of data storage. First I tried map, which had poor performance, and then I tried array. The performance improvement was not significant.
//map public String intToRoman(int num) { Map<Integer, Character> romanIntMap = new HashMap<Integer, Character>(); romanIntMap.put(1, 'I'); romanIntMap.put(5, 'V'); romanIntMap.put(10, 'X'); romanIntMap.put(50, 'L'); romanIntMap.put(100, 'C'); romanIntMap.put(500, 'D'); romanIntMap.put(1000, 'M'); StringBuilder finalResult = new StringBuilder(); for(int i = 1 ; num>0 ; i*=10){ StringBuilder result = new StringBuilder(""); int digit = num%10; if(digit==0){ }else if(digit<=3){ for(int j = 0 ; j<digit ; j++){ result.append(romanIntMap.get(i)); } }else if(digit==4){ result.append(romanIntMap.get(i*5)); result.append(romanIntMap.get(i)); }else if(digit==5){ result.append(romanIntMap.get(i*5)); }else if(digit<=8){ for(int j = 0 ; j<digit-5 ; j++){ result.append(romanIntMap.get(i)); } result.append(romanIntMap.get(i*5)); }else{ result.append(romanIntMap.get(i*10)); result.append(romanIntMap.get(i)); } finalResult.insert(0,result.reverse()); num /= 10; } return finalResult.toString(); } //array public String intToRoman2(int num){ char[] romans = new char[]{'I','V','X','L','C','D','M'}; StringBuilder finalResult = new StringBuilder(); for(int i = 0 ; num>0 ; i++){ StringBuilder result = new StringBuilder(""); int digit = num%10; num /= 10; if(digit==0){ continue; }else if(digit<=3){ for(int j = 0 ; j<digit ; j++){ result.append(romans[i*2]); } }else if(digit==4){ result.append(romans[i*2+1]); result.append(romans[i*2]); }else if(digit==5){ result.append(romans[i*2+1]); }else if(digit<=8){ for(int j = 0 ; j<digit-5 ; j++){ result.append(romans[i*2]); } result.append(romans[i*2+1]); }else{ result.append(romans[i*2+2]); result.append(romans[i*2]); } finalResult.insert(0,result.reverse()); } return finalResult.toString(); }
Idea 2: Exhaust Enumeration
Because the input of Roman numerals in the question setting is a value in a certain interval, its special situation can be exhausted.
Let's start from the individual position and consider the special situation of each one.
==9 : IX
==5 : V
==4 : IV
<4 : I*n
That is to say, all the remaining values subtracted from large to small have corresponding Roman numerals.
public String intToRoman(int num) { int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; String[] strs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; StringBuilder sb = new StringBuilder(); for(int i=0;i<values.length;i++) { while(num >= values[i]) { num -= values[i]; sb.append(strs[i]); } } return sb.toString(); }