解題思路,有二條處理邏輯:
一,判斷當前字符是否比next字符小,如果小的話需要用下一個字符減去當前字符加到最終結果字符身上。
二,否則的話,就把當前字符直接加到最終結果字符。
下面是AC代碼:
public class Solution {
HashMap romanToIntMap = new HashMap() {
{
put('I', new Integer(1));
put('V', new Integer(5));
put('X', new Integer(10));
put('L', new Integer(50));
put('C', new Integer(100));
put('D', new Integer(500));
put('M', new Integer(1000));
}
};
public int romanToInt(String s) {
int ret = 0;
int i = 0;
while(i