上一题:LeetCode第12题: intToRoman(C语言)
int romanToInt(char* s) {
int result = 0;
if(strlen(s) == 0)
return result;
int index = 0;
while(s[index] != '\0'){
char cur = s[index];
char next_cur = s[index + 1];
if(cur == 'C' && next_cur == 'M'){
result += 900;
index += 2;
}
else if(cur == 'C' && next_cur == 'D'){
result += 400;
index += 2;
}
else if(cur == 'X' && next_cur == 'C'){
result += 90;
index += 2;
}
else if(cur == 'X' && next_cur == 'L'){
result += 40;
index += 2;
}
else if(cur == 'I' && next_cur == 'X'){
result += 9;
index += 2;
}
else if(cur == 'I' && next_cur == 'V'){
result += 4;
index += 2;
}
else{
switch(cur){
case 'M':
result += 1000;
break;
case 'D':
result += 500;
break;
case 'C':
result += 100;
break;
case 'L':
result += 50;
break;
case 'X':
result += 10;
break;
case 'V':
result += 5;
break;
case 'I':
result += 1;
break;
}
index += 1;
}
}
return result;
}
本系列文章,旨在打造LeetCode题目解题方法,帮助和引导同学们开阔学习算法思路,由于个人能力和精力的局限性,也会参考其他网站的代码和思路,如有侵权,请联系本人删除。
下一题:LeetCode第14题: longestCommonPrefix(C语言)