Total Accepted: 56002
Total Submissions: 159037
Difficulty: Easy
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Show Similar Problems
public class Solution {
public int romanToInt(String s) {
String roman = "MDCLXVI";
int[]Int={1000,500,100,50,10,5,1};
int result = 0;
for(int i=0;i<s.length()-1;i++){
if(roman.indexOf(s.charAt(i))>roman.indexOf(s.charAt(i+1))){
result-=Int[roman.indexOf(s.charAt(i))];
}
else{
result+=Int[roman.indexOf(s.charAt(i))];
}
}
result+=Int[roman.indexOf(s.charAt(s.length()-1))];
return result;
}
}
本文介绍了一种将罗马数字转换为整数的算法实现。输入范围保证在1到3999之间,通过查找和比较字符在罗马数字字符串中的位置来确定其对应的整数值,最终返回转换后的整数。

被折叠的 条评论
为什么被折叠?



