赞
踩
题目:
题解:
- class Solution {
- private:
- unordered_map<char, int> symbolValues = {
- {'I', 1},
- {'V', 5},
- {'X', 10},
- {'L', 50},
- {'C', 100},
- {'D', 500},
- {'M', 1000},
- };
-
- public:
- int romanToInt(string s) {
- int ans = 0;
- int n = s.length();
- for (int i = 0; i < n; ++i) {
- int value = symbolValues[s[i]];
- if (i < n - 1 && value < symbolValues[s[i + 1]]) {
- ans -= value;
- } else {
- ans += value;
- }
- }
- return ans;
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。