(来自力扣算法第13题)
罗马数字包含以下七种字符: I
, V
, X
, L
,C
,D
和 M。
class Solution {
//把罗马数字存入map表中
Map<Character, Integer> romanValue = new HashMap<Character, Integer>() {
{
put('I', 1);
put('V', 5);
put('X', 10);
put('L', 50);
put('C', 100);
put('D', 500);
put('M', 1000);
}};
public int romanToInt(String s) {
int num=0; //用来存储罗马数字
int n=s.length(); //取出罗马字符串的长度
for(int i=0;i<n;i++){ //遍历字符
int value=romanValue.get(s.charAt(i)); //遍历的字符对应map表中的数字
if(i<n-1 &&a