[LeetCode #13] Roman to Integer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 13. Roman to Integer(python)

leetcode-13-Roman to Integer

#Leetcode# 13. Roman to Integer

[leetcode] 13. Roman to Integer

LeetCode 13. Roman to Integer

Leetcode 13. Roman to Integer