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

Posted yangnk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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/的主要内容,如果未能解决你的问题,请参考以下文章

栈用俩个栈来实现队列(剑指Offer 09)

栈用俩个栈来实现队列(剑指Offer 09)