This is the 10th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
🌺 topic:
Roman numerals contain the following seven characters: I, V, X, L, C, D and M.
I 1 V 5 X 10 L 50 C 100 D 500 M 1000Copy the code
For example, the Roman numeral 2 is written II, which means two 1s side by side. Let’s write 12 as XII, which is X plus II. 27 write XXVII, that is XX
- V + II.
In Roman numerals, the smaller number is usually placed to the right of the larger number. But there are exceptions. For example, instead of writing IIII for 4, I would write IV. The number 1 is to the left of the number 5, which is equal to the large number 5 and the number 4 is obtained by reducing the number 1. Similarly, the number 9 is represented as IX. This special rule only applies in the following six cases: I can be placed to the left of V (5) and X (10) to represent 4 and 9. X can be placed to the left of L (50) and C (100) to represent 40 and 90. C can be placed to the left of D (500) and M (1000) to represent 400 and 900. Give you a whole number, convert it to Roman numerals.Copy the code
Example 1: Input: num = 3 Output: "III"Copy the code
Example 2: Input: num = 4 Output: "IV"Copy the code
Example 3: Input: num = 9 Output: "IX"Copy the code
Example 4: Input: num = 58 Output: "LVIII" Description: L = 50, V = 5, III = 3.Copy the code
Example 5: Input: num = 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90, IV = 4.Copy the code
Tip: 1 <= num <= 3999 Passes 213,110 commits 320,090Copy the code
🌺 A little thought
After reading these three days to do the problem, I think the problem is still ok, not to say that you see no clue but to look at a little train of thought, you can follow their own train of thought to write down. ** Let’s not do too difficult problems in the early stage to avoid hurting our confidence. ** Well, if I look at this problem, the way I think about it is that he says there are special values
I can be placed to the left of V (5) and X (10) to represent 4 and 9. X can be placed to the left of L (50) and C (100) to represent 40 and 90. C can be placed to the left of D (500) and M (1000) to represent 400 and 900.Copy the code
So we’re going to treat them as special just like we did with the normal characters, we’re going to use a hash table to set a key, and the other thing to notice is that there are many different ways that a hundred could be made up of ten tens but we’re going to have to choose C
Open dry 🌺
🌺 introduces today’s functions
If you don’t know the difference between a HashMap and a HashSet, you might want to ask about it in the interview. We’re going to introduce HashMap today and we’re going to use it in a moment to construct enantiomers between numbers and characters.
- clear()
Clear () clears the HashMap. This is done by setting all elements to null.
- containsKey(key)
ContainsKey () determines whether a HashMap contains a key.
- containsValue(value)
ContainsValue () determines if the HashMap contains an element with a value of value.
- EntrySet (), values(), keySet()
EntrySet () returns “a collection of all entries in a HashMap,” which is a collection.
- get(key)
Get () is used to retrieve the value corresponding to the key
- put(key,value)
The function of put() is to provide an interface for HashMap objects to add key-values to a HashMap.
- putAll(map)
PutAll () adds all the elements of “m” to the HashMap
- remove(key)
Remove () removes the element whose key is key
- clone()
Clone a HashMap and return an Object and I think I’d be happy to understand a function that I don’t know how to do. Ok? Another suggestion is to compare these to hashset memorization.
🌺 source code and detailed explanation
It’s kind of embarrassing because I wanted to use HashMap for one key for one value, but when I started, I realized that I didn’t need it. Ha, ha, ha. Let’s put it there.
public String luoma(int num) {
int[] a= {1000.900.500.400.100.90.50.40.10.9.5.4.1};// Make the special one a key-value pair
String[] b= {"M"."CM"."D"."CD"."C"."XC"."L"."XL"."X"."IX"."V"."IV"."I"};
// Use a pointer to control the array elements
int j=0;
// Store the final output
String s="";
for(int i:a) {
//ans = num/a =4; //ans = num/a =4
int ans=0;
if(num! =0) {
ans=(int)num/i;
String xue="";[j] [J]
for(int fei=0; fei<ans; fei++) { xue=xue+b[j]; } s=s+xue;// Merge the result strings
num=num%i;// Get rid of the above
j++;// Move the pointer}}return s;
}
Copy the code
Well, today’s algorithm problem here, we see you tomorrow!