leetcode 罗马转换

import java.sql.SQLClientInfoException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap.KeySetView;

import javax.print.DocFlavor.STRING;
public class huiWen {
public int num1 (String luoma) {
Map map= new HashMap<>();
map.put("I", 1);
map.put("V", 5);
map.put("X", 10);
map.put("L", 50);
map.put("C", 100);
map.put("D", 500);
map.put("M", 1000);
char[] c1 = luoma.toCharArray();
//System.out.println("长度为:"+luoma.length());
int sum = 0;
for (int i = 0; i < c1.length-1; i++) {
//System.out.println( String.valueOf(c1[0]));
//System.out.println(map.get(String.valueOf(c1[0])));
if (map.get(String.valueOf(c1[i])) < map.get(String.valueOf(c1[i+1]))) {
sum = sum - map.get(String.valueOf(c1[i]));
}
else {
sum = sum +map.get(String.valueOf(c1[i]));
}
}

return sum;
}

public static void main(String[] args) {
huiWen s1 = new huiWen();
System.out.print(s1.num1("MC"));
}
}

 

 

总是少一位,参考了各种答案,还没看到完美的

 

你可能感兴趣的:(leetcode 罗马转换)