码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode #13] Roman to Integer

时间:2016-10-02 00:09:35      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/amadis/p/5926477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!