The title

LeetCode 13 questions

Roman numerals contain the following seven characters: I, V, X, L, C, D and M. Character value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, the Roman numeral 2 is written as II, which is the parallel 1. 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 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 put to the left of D (500) and M (1000) to represent 400 and 900. Given a Roman numeral, convert it to an integer. Make sure the input is in the range of 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Description: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90, IV = 4. Tip: 1 < = s.l ength < = 15 s only contain characters (' I ', 'V' and 'X', 'L', 'C', 'D', 'M') subject data guarantee s is a valid Roman numerals, It indicates that the integer is in the range [1, 3999] and all the test cases given in the question comply with the Roman numerals writing rules, and there will be no cross. 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.Copy the code

Time to solve the problem.

class Solution {
    public int romanToInt(String s) {
    
    
      }
}
Copy the code

The method input parameters are given above to complete the answer.

Subject analysis

  1. Roman numerals consist of I,V,X,L,C,D,M;
  2. When the small value is to the left of the large value, the reduced value, such as IV=5-1=4;
  3. If the small value is to the right of the large value, the small value is added, for example, VI=5+1=6;
  4. The rvalue is always positive, so the last digit must be positive.
  5. In short, putting a small value to the left of a large value is subtraction, otherwise addition.

Answers to analysis

This article only analysis I do the idea, only for reference, to understand a solution to the idea, other kinds of ideas to do the problem please access the Internet.

Answer success:

Execution time :18 ms, beating 5.03% Java users memory consumption :39.1 MB, beating 16.51% Java usersCopy the code

This time I did not think of a good method, so it is inefficient to correspond single characters and special cases, and then convert strings into characters and then convert them.

This answer is accompanied by recommended answers, you can directly read the view!

Public class Solution {public int romanToInt(String s) { Integer> map = new HashMap<>(); map.put("IV", 4); map.put("I", 1); map.put("V", 5); map.put("X", 10); map.put("IX", 9); map.put("XL", 40); map.put("L", 50); map.put("XC", 90); map.put("CD", 400); map.put("CM", 900); map.put("C", 100); map.put("D", 500); map.put("M", 1000); Char [] chars = s.tochararray (); int result = 0; boolean isTow = false; // loop conversion for (int I = 0; i < chars.length; i++) { if (isTow) { isTow = false; continue; } if (i + 1 < chars.length) { if (map.get(String.valueOf(chars[i]) + String.valueOf(chars[i + 1])) ! = null) { isTow = true; result += map.get(String.valueOf(chars[i]) + String.valueOf(chars[i + 1])); } else { isTow = false; result += map.get(String.valueOf(chars[i])); } }else { result += map.get(chars[i]+""); } } return result; }}Copy the code

Recommend the answer

Answer success: Class Solution {public int romanToInt(String s) {char[] chars = s.toCharArray(); char preNum = s.charAt(0); int result = getNum(preNum); for (int i = 1; i < chars.length; i++) { result += getNum(chars[i]); if (getNum(preNum) < getNum(chars[i])) { result -= getNum(preNum) * 2; } preNum = chars[i]; } return result; } private int getNum(char code) { switch (code) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: return 0; }}}Copy the code