首页 > 代码库 > [LeetCode #13] Roman to Integer

[LeetCode #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.

 

 1 int c2n(char c){ 2     switch (c){ 3         case I: return 1;  4         case V: return 5;   5         case X: return 10;   6         case L: return 50;   7         case C: return 100;   8         case D: return 500;   9         case M: return 1000;  10         default: return 0;  11     }  12 }13 14 int romanToInt(char* s) {15     int result = 0;16     for(int i = 0; i < strlen(s); i++){17         if ( i>0 && (c2n(s[i]) > c2n(s[i-1]))){18             result += (c2n(s[i]) - 2 * c2n(s[i-1]));19         }else{20             result += c2n(s[i]);   21         }22     }23     24     return result;25 }

 

[LeetCode #13] Roman to Integer