The topic is simple, but it is impossible to understand the relationship between numbers and Roman numerals.
Title:
principles and ideas:Roman numerals have the following symbols:
Basic characters |
I |
V |
X |
L |
C |
D |
M |
corresponding Arabic numerals |
1 |
5 |
10 |
50 |
100 |
500 |
1000 |
count rule:
- The same number ligatures, the number represented is equal to the number that these numbers are added to, for example: III = 3
- The small number is to the right of the large number, and the number represented is equal to the number added to the number, for example: VIII = 8
- Small numbers, limited to (I, X, and C) to the left of large numbers, represented by numbers equal to large numbers minus decimals, for example: IV = 4
- In normal use, no more than three consecutive digits must be repeated.
- Draw a line on the top of a number, indicating that the number is enlarged by 1000 times times (the subject only takes into account 3999 or less, so this rule is not used)
Secondly, the Roman numeral to Arabic numerals rule (limited to 3999):The Roman numerals are traversed backwards, and if a number is smaller than the previous one, the number is added. Conversely, subtract twice times the previous number and then add that number.
Code Scenarios:
Import Java.util.map;import Java.util.hashmap;public class Solution {public int romantoint (String s) {map< Character,integer> map=new hashmap<character,integer> () map.put (' I ', 1); Map.put (' V ', 5); Map.put (' X ', 10); Map.put (' L ', '); Map.put (' C ', +); Map.put (' D ', '); Map.put (' M ', +) ; int I, total, pre, cur; Total = Map.get (S.charat (0)); for (i = 1; i < s.length (); i++) { pre = Map.get (S.charat (i-1)); cur = map.get (S.charat (i)); if (cur <= pre) {total + = cur, } else {Total = Total-pre * 2 + cur; } } return total; }}
Leetcode--roman to Integer