首页 > 代码库 > Leetcode#13 Roman to Integer

Leetcode#13 Roman to Integer

原题地址

 

罗马数字的问题可以参考这篇博文,讲的很清楚

 

代码:

 1 int transfer(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 -1;11   }12 }13     14 int romanToInt(string s) {15   int res = 0;16   int pre = 0;17   for (int i = 0; i < s.length(); i++) {18     int curr = transfer(s[i]);19     if (curr <= pre)20       res += curr;21     else {22       res -= pre * 2;23       res += curr;24     }25     pre = curr;26   }27         28   return res;29 }

 

Leetcode#13 Roman to Integer