首页 > Web开发 > 详细

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

时间:2021-08-17 10:24:09      阅读:27      评论:0      收藏:0      [点我收藏+]
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/

原文:https://www.cnblogs.com/yangnk/p/15150476.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!