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.
水题一枚,注意搞清楚规则,注意空间分配
class Solution { public: int romanToInt(string s) { int length = s.size(); int p[length + 1] = {0}; int num = 0; for(int i = 0;i < length;i++) { switch(s[i]) { case 'I': p[i] = 1; break; case 'X': p[i] = 10; break; case 'C': p[i] = 100; break; case 'M': p[i] = 1000; break; case 'V': p[i] = 5; break; case 'L': p[i] = 50; break; case 'D': p[i] = 500; break; default: p[i] = 0; break; } } for(int i = 0;i < length;i++) { if(p[i] >= p[i + 1]) { num = num + p[i]; } if(p[i] < p[i + 1]) { num = num - p[i]; } } return num; } };