Author: hancao
Introduction: Front-end engineer of benming Year
WeChat: hancao97
background
I began to ask for leave in the Spring Festival since I was a child, and had a complete rest for two weeks (if this article is read, please put the envy of the two words in the comment, haha). The two weeks I intended to study hard and make progress every day, and work hard to build a strong body. And then it turned out to be bullshit!! Study at home for Chinese New Year? It doesn’t exist. I spend every day with my parents and my family, and I play with my kids. Oddly enough, the original university of time, I don’t like children, but last year, this year go home I feel good and lovely children, wow, so the clear eyes, so lovely, small hand and feet is really let me this old man had heart melted, alas, I most like a baby ~ (laws of all escape but delicious, I also like ~)
So, since rest for so long, my heart would have gone with the wind outside the cloud nine, I need to adjust their own state to start the New Year, New Year must be brimming with vigour, do so from the two leetcode begin, but I’m not add hands gave birth to this strength, must choose simple (choose simple said justified), as a result, This Roman numeral and the interchangeover of numerals is very much in my heart, so go!!
Roman numerals and integer conversion rules
First of all, LET me introduce Roman numerals, which is also convenient for you to understand the following topic.
Roman numerals contain the following seven characters: I, V, X, L, C, D and M.
Character values I 1 V 5 X 10 L 50 C 100 D 500 M 1000Copy the code
This is Roman numerals and integers.
Usually, the smaller Roman numerals are to the right of the larger numerals. But there are exceptions, for example, 4 is not written as IIII, it’s written as IV. The number 1 is to the left of the number 5 and represents the number 4 when the larger number 5 decreases by 1. Similarly, the number 9 represents IX. This particular rule applies only to the following six situations:
I can be put 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 put to the left of D (500) and M (1000) to represent 400 and 900.Copy the code
So, we can actually change this table.
Character values I 1 IV 4 V 5 IX 9 X 10 XL 40 L 50 XC 90 C 100 CD 400 D 500 CM 900 M 1000Copy the code
Ok, now that we have this mapping rule, we can start doing these two algorithms!
Convert Roman numerals to integers
Let’s start with simple Roman numerals rounded.
Here are a few examples:
Input: "III" Output: 3 Input: "IV" Output: 4 Input: "LVIII" Output: 58 Description: L = 50, V= 5, III = 3. Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90, IV = 4.Copy the code
Ok, this problem is actually very simple, the overall idea is a traversal of the input string, in the traversal process there are two cases:
- We subtract when the previous Roman character is smaller than the integer corresponding to the next Roman character
- We add when the previous Roman character is greater than or equal to the integer corresponding to the next Roman character
Ok! Since the idea is so simple, let’s go straight to the code
Why didn’t the nuggets code paste over the line? Finally, I took a screenshot.)
Running results:
Not so good, actually. I’m sad.
Here’s my face…
Integer to Roman numerals
This is a little harder than the previous problem, and leetCode’s difficulty has changed from easy to medium, which is actually good because we have a new mapping as mentioned above!
Character values I 1 IV 4 V 5 IX 9 X 10 XL 40 L 50 XC 90 C 100 CD 400 D 500 CM 900 M 1000Copy the code
Let’s start with an example:
Output: "III" Input: 4 Output: "IV" Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Description: M = 1000, CM = 900, XC = 90, IV = 4.Copy the code
First we have to think about greedy algorithms, and the idea is this:
Each step is matched with the largest Roman character of the current corresponding integer, resulting in the smallest Roman numeral representation.
All right, let’s do it:
Var intToRoman = function (num) {const NUMS =,90,50,40,10,9,5,4,1 [1000900500400100]; const ROMON_CHARS = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']; let index = 0; let romonResStr = ''; let temp = 0; while(index < 13 && num > 0) { temp = Math.floor(num/NUMS[index]); if(temp>0){ romonResStr += ROMON_CHARS[index].repeat(temp); num -= NUMS[index]*temp; } index++; } return romonResStr; };Copy the code
So just to give you a little bit of code, first of all we’re going to match from the larger Roman characters, so I’ve created two arrays of NUMS and ROMON_CHARS Roman characters, from the largest to the smallest, and why I chose arrays, because I care about their order, unlike the previous algorithm, So the data structure I used for that algorithm was Map.
Then we do some initialization, index corresponds to the subscript of NUMS and ROMON_CHARS, romonResStr is the result string, and the temp variable.
The loop then begins, ending with index>=13 or num<=0. Temp >0; temp>0; temp>0; temp>0; temp>0; temp>0; temp>0 If temp>0, we repeat the current Roman character temp times, append it to romonResStr, and execute num -= NUMS[index]*temp. Finally index++ enters the next loop.
The final result would look something like this:
Ok, that’s it for this code. These two questions are still relatively simple, suitable for New Year practice hands ~
Review flags and summaries
Look at this, learn that and that, emMM, carry the following, list them:
Be an independent, executive engineer. As of January 2021, get up at 6:30, study for at least two hours a day, and exercise 30-90 minutes a day (at least five times a week). In-depth study of basic computer knowledge. (long term, not limited to 2021) VuE3 source code learning. 【 English level needs to be improved... 】 To enhance your ability to obtain quality materials and learn. Output 12 high-quality blogs. 100 leetCode algorithm questions [Completed 15 in January] improve the level of algorithms and data structures [read systematically] (long term, not limited to 2021) go to Tibet! Turn into light, Deja! Sorry, not serious!Copy the code
In fact, there are many flags in the forefront, but as an engineer, I have been working for half a year, and I feel that this is actually work, and there are corresponding responsibilities at work, so I briefly convert the goals into the following points:
1. Provide high-quality and efficient business support in work (sense of responsibility is the most basic thing to be done) 2. Be positive in your life and do things you love (like travel!). And do it! 3. Personal growth should not be limited, expanded and deepened. Learning is not limited to the front end, but also includes computer foundation and various soft skills. And harvest love is the best, but I feel I am still young, not in a hurryCopy the code
Best wishes
Wish you all the best!
Wish you all good health!
Wish everyone a happy heart!
I hope everyone has confidence and courage!
Hope everyone can know more people with temperature!
I hope you all have a passion and a goal to pursue!
Hope everyone (mostly me) finds the one!
Wish world peace!
.
New Year’s work begins!!
Focus!
Come on!