public class Solution {
public int romanToInt(String s) {
int len = s.length();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
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);
int total = map.get(s.charAt(0));
for(int i = 1;i < len;i++) {
if(map.get(s.charAt(i)) > map.get(s.charAt(i-1))) {
total = total + map.get(s.charAt(i)) - 2*map.get(s.charAt(i - 1));
}else{
total = total + map.get(s.charAt(i));
}
}
return total;
}
}
leetcode-java-13. Roman to Integer
最新推荐文章于 2022-03-19 13:18:35 发布