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/

上一篇:ctfshow_吃鸡杯__web_两题


下一篇:C++ 自动重启程序