# [LeetCode] Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Subscribe to see which companies asked this question

public class Solution {
public int romanToInt(String s) {
int ret = charToNum(s.charAt(0));

for (int i = 1; i < s.length(); i++) {
if (charToNum(s.charAt(i-1)) < charToNum(s.charAt(i)))
ret += charToNum(s.charAt(i)) - 2*charToNum(s.charAt(i-1));
else
ret += charToNum(s.charAt(i));
}
return ret;
}

public int charToNum (char c) {
switch(c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
}

#### Integer to Roman （罗马数字转换） 【leetcode】

2013-09-20 16:09:30

#### 【LeetCode013算法/编程练习C++】Roman to Integer //难得击败了80%的人

2016-12-29 19:37:13

#### [LeetCode]Roman to Integer,解题报告

2013-12-01 20:46:29

#### leetcode-java-13. Roman to Integer

2016-06-02 17:09:11

#### LeetCode 12 Integer to Roman(C,C++,Java,Python)

2015-05-08 13:13:53

#### LeetCode --- 12. Integer to Roman

2015-01-29 10:49:53

#### 【LeetCode】Integer to Roman 和 Roman to Integer 解题报告

2014-10-10 20:28:24

#### LeetCode(13) RomanToInteger

2013-12-11 10:42:45

#### [leetcode]Roman to Integer C语言

2015-08-20 09:49:08

#### [LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

2015-03-02 23:19:35