https://leetcode-cn.com/problems/roman-to-integer/submissions/

package oj;

import java.util.HashMap;
import java.util.Map;

public class RomanToInteger0816 {
Map<Character, Integer> characterIntegerHashMap = new HashMap() {
{
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 len = s.length();
int ans = 0;
for (int i = 0; i < len; i++) {
int value = characterIntegerHashMap.get(s.charAt(i));
if (i < len - 1 && (value < characterIntegerHashMap.get(s.charAt(i + 1)))) {
ans -= value;
} else {
ans += value;
}
}
return ans;
}

public static void main(String[] args) {
RomanToInteger0816 romanToInteger0816 = new RomanToInteger0816();
System.out.println(romanToInteger0816.romanToInt("IX"));
}
}

https://leetcode-cn.com/problems/roman-to-integer/submissions/

[db:回答]

以上是https://leetcode-cn.com/problems/roman-to-integer/submissions/的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>