LeetCode - 13. 罗马数字转整数
2018-09-08 12:38
513 查看
13. 罗马数字转整数
import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; class Solution { private static boolean isSpecial(Character a, Character b) { if (a.equals('I') && (b.equals('V') || b.equals('X'))) { return true; } if (a.equals('X') && (b.equals('L') || b.equals('C'))) { return true; } if (a.equals('C') && (b.equals('D') || b.equals('M'))) { return true; } return false; } public static int romanToInt(String s) { Map<Character, Integer> map = new HashMap<>(); map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put('C', 100); map.put('D', 500); map.put('M', 1000); Queue<Character> queue = new LinkedList<>(); char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; ++ i) { queue.add(chars[i]); } int result = 0; while (! queue.isEmpty()) { Character one = queue.poll(); if (! queue.isEmpty()) { Character two = queue.peek(); &nb b68 sp; if (isSpecial(one, two)) { queue.poll(); result += map.get(two) - map.get(one); continue; } } result += map.get(one); } return result; } public static void main(String[] args) { System.out.println(romanToInt("III")); } }
相关文章推荐
- Leetcode : 13 罗马数字转整数
- LeetCode-13罗马数字转整数
- Leetcode 13 Roman to Integer 罗马数字转整数
- LeetCode习题笔记——罗马数字和整数之间的转换
- 【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】
- LeetCode 13. Roman to Integer 罗马数字转整数
- leetcode 13 罗马数字转10进制
- leetcode_13(罗马数字变成十进制)
- leetCode 12.Integer to Roman (整数转罗马数字) 解题思路和方法
- LeetCode : 12 整数转罗马数字 C++
- Leetcode简单题罗马数字转整数
- 【Leetcode-13】罗马数字转换为阿拉伯数字
- leetcode-java.T013_RomarToInteger 罗马数字转成整数数字
- LeetCode 13. 罗马数字转整数
- LeetCode 12 Integer to Roman (整数转罗马数字)
- [LeetCode] Roman to Integer 罗马数字转化成整数
- [LeetCode] Integer to Roman 整数转化成罗马数字
- LeetCode 罗马数字转整数
- Leet Code 13 Roman to Integer - 罗马数字转成整数 - Java
- leetcode:Integer to Roman(整数转化为罗马数字)