13. Roman to Integer【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.

 public class Solution {
public int romanToInt(String s) {
if (s == null || s.length()==0) {
return 0;
}
Map<Character, Integer> m = new HashMap<Character, Integer>();
m.put('I', 1);
m.put('V', 5);
m.put('X', 10);
m.put('L', 50);
m.put('C', 100);
m.put('D', 500);
m.put('M', 1000); int length = s.length();
int result = m.get(s.charAt(length - 1));
for (int i = length - 2; i >= 0; i--) {
if (m.get(s.charAt(i + 1)) <= m.get(s.charAt(i))) {
result += m.get(s.charAt(i));
} else {
result -= m.get(s.charAt(i));
}
}
return result;
}
} // 方法二
public class Solution {
/**
* @param s Roman representation
* @return an integer
*/
public int romanToInt(String s) {
// Write your code here
int ans;
char[] sc = s.toCharArray();
ans = toInt(sc[0]); //0 special
for (int i = 1; i < s.length(); i++) {
ans += toInt(sc[i]);
if (toInt(sc[i - 1]) < toInt(sc[i])) {
ans -= toInt(sc[i - 1]) * 2;
}
}
return ans;
} int toInt(char s) {
switch(s) {
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;
}
return 0;
}
}
上一篇:Facebook interview problem:13. Roman to Integer


下一篇:<> Chapter 3:Char Drivers