LeetCode 13. Roman to Integer
程序员文章站
2024-02-03 17:03:16
...
LeetCode 13. Roman to Integer
Solution1:貌似是我的答案,还是参考的谁的??
和12题相反的一个题
class Solution {
public:
int romanToInt(string s) {
if (s.length()==0) return 0;
unordered_map<char, int> m ={{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
int n = s.length();
int result = m[s[n-1]];
for (int i=n-2; i>=0; i--) {
if (m[s[i]] >= m[s[i+1]])
result += m[s[i]];
else
result -= m[s[i]];
}
return result;
}
};
推荐阅读
-
LeetCode 13. Roman to Integer
-
LeetCode—— 13. Roman to Integer
-
LeetCode_#7_整数反转 Reverse Integer_C++题解
-
Leetcode rever-Integer
-
Leetcode解题 7. Reverse Integer 反转整数
-
Leetcode 13. Roman to Integer
-
Leetcode 7. Reverse Integer
-
LeetCode:7. Reverse Integer
-
【leetcode】7. Reverse Integer
-
【数据结构】散列表:LeetCode题(二)1. 两数之和,13. 罗马数字转整数