This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021


Write in front:

  • Personal front-end website: Zhangqiang.hk.cn
  • Welcome to join the blogger front-end learning QQ exchange group: : 706947563, focus on front-end development, learning and progress together!

1 the title

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.

** characters ** ** Value ** I 1 V 5 X 10 L 50 C 100 D 500 M 1000Copy the code

For example, the Roman numeral 2 is written as II, which is two ones side by side. Write XII as X + II. Write XXVII as XX + V + II.

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.

Given a Roman numeral, convert it to an integer.

Example 1:

Input: s = "III" Output: 3Copy the code

Example 2:

Input: s = "IV" Output: 4Copy the code

Example 3:

Input: s = "IX" Output: 9Copy the code

Example 4:

Input: S = "LVIII" Output: 58 Interpretation: L = 50, V= 5, III = 3Copy the code

Example 5:

Input: S = "MCMXCIV" Output: 1994 Interpretation: M = 1000, CM = 900, XC = 90, IV = 4Copy the code

Tip:

  • 1 <= s.length <= 15
  • S contains only characters (‘I’, ‘V’, ‘X’, ‘L’, ‘C’, ‘D’, ‘M’)
  • The data guarantees that s is a valid Roman numeral and represents an integer within the range [1, 3999]
  • All test cases are in accordance with the Roman numerals writing rules, and there will be no cross-position.
  • Examples like IL and IM don’t fit the question, 49 should be XLIX, 999 should be CMXCIX.
  • For detailed rules for writing Roman numerals, see Roman numerals – Mathematics.

2 Personal analysis

2.1 train of thought

The first time I saw this question in my mind: Roman numerals? I know I, II, III, oh, I think there’s an IV there, how can I say more? Let me see what it is.

After seeing a lot of eyes: oh ~~~ so ah, then how should I express the relationship between these numbers? How do you compare them to each other? Well, you can create an object, and then the key of the object will be the token, and the value will be the number value. Okay, show code~

2.2 code

/** * @param {string} s * @return {number} */ var romanToInt = function(s) { let obj = { 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000 }; let sum=0; for(let i=0; i<s.length; i++){ if(i+1<s.length && obj[s[i]]<obj[s[i+1]]){ sum=sum+obj[s[i+1]]-obj[s[i]]; i++; continue; } sum=sum+obj[s[i]]; } return sum; };Copy the code

3 React ref Example

The focus() event can only be called from the input element. Use the react ref to call the focus() event. Button onClick(), or any other component. Here’s the official code:

import React,{useState,useRef,useEffect} from "react"; function TextInputWithFocusButton() { const inputEl = useRef(null); Const onButtonClick = () => {// 'current' points to the text input element inputel.current.focus () mounted to the DOM; }; return ( <> <input ref={inputEl} type="text" /> <button onClick={onButtonClick}>Focus the input</button> </> ); }Copy the code