[LintCode] Roman to Integer

Given a roman numeral, convert it to an integer.

The answer is guaranteed to be within the range from 1 to 3999.

Clarification

What is Roman Numeral?

Example

IV -> 4

XII -> 12

XXI -> 21

XCIX -> 99

Observation:  if s.charAt(i) > s.charAt(i - 1), it means we need to subtract the mapped value of s.charAt(i - 1) from the mapped value of s.charAt(i).

Based on this observation, the following algorithm is developed.

1. init result to m.get(s.charAt(length - 1)), the mapped value of the last character.

2. starting from the 2nd last character, iterate through the string from right to left; if the current character‘s mapped value is smaller than

its right neighbor‘s, subtract this mapped value from the result; if not, add this mapped value to the result.

 1 public class Solution {
 2      public int romanToInt(String s) {
 3         if (s == null || s.length()==0) {
 4                 return 0;
 5         }
 6         Map<Character, Integer> m = new HashMap<Character, Integer>();
 7         m.put(‘I‘, 1);
 8         m.put(‘V‘, 5);
 9         m.put(‘X‘, 10);
10         m.put(‘L‘, 50);
11         m.put(‘C‘, 100);
12         m.put(‘D‘, 500);
13         m.put(‘M‘, 1000);
14
15         int length = s.length();
16         int result = m.get(s.charAt(length - 1));
17         for (int i = length - 2; i >= 0; i--) {
18             if (m.get(s.charAt(i + 1)) <= m.get(s.charAt(i))) {
19                 result += m.get(s.charAt(i));
20             } else {
21                 result -= m.get(s.charAt(i));
22             }
23         }
24         return result;
25     }
26 }

Related Problems

Integer to Roman

时间: 2024-10-18 19:05:04

[LintCode] Roman to Integer的相关文章

[LintCode] Roman to Integer 罗马数字转化成整数

Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Have you met this question in a real interview? Yes Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals htt

[lintcode medium]Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Example IV -> 4 XII -> 12 XXI -> 21 XCIX -> 99 Clarification What is Roman Numeral? https://en.wikipedia.org/wiki

LeetCode 013 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [题意] 把罗马数转换为整数 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 大体思路是每个罗马字母对应的值相加即可, 但需要处理900, 400, 90, 40, 9, 4这几个特殊值(左

leetCode 13. Roman to Integer 字符串

13. Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 补充:罗马数字 罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

Roman to Integer——罗马数字转变算法

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41486885 通过本文你可能学到的知识如下: (1)理解本题的解题思路,在以后类似的场景中,如果没有想到比较好的方法,可以考虑使用本文的方法,虽然效率不是特别高. (2)能够对字符串的截取和HashMap相关操作有所学习. Roman to Integer Given a roman numeral, convert it to an integer. I

LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则.Wiki了一把,又参考了其它的文档,总结如下: 罗马数字规则: 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 罗马数字中没有"0". 2, 重复次数:一个罗马数字最多重复3次. 3, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上

【leetcode】Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 罗马数字有如下符号: 基本字符 I V X L C D M 阿拉伯数字 1 5 10 50 100 500 1000 计数规则: 相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3 小的数字在大的数字右边,所表示的数等于这些数字

Leetcode | Roman to Integer &amp; Integer to Roman

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 搜了一下,才知道这道题可以写这么简洁. 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 if (s.empty()) return 0; 5 int dict[100]; 6 d

58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字,转换为阿拉伯数字.本题只考虑3999以内的数. 罗马数字有如下符号: Ⅰ(1)Ⅴ(5)Ⅹ(10)L(50)C(100)D(500)M(1000) 计数规则: (1).若干相同数字连写表示的数是这些罗马数字的和,如III=3: (2).小数字在大数字前面表示的数是用大数字减去小数字,如IV=4: (3