diff --git a/LeetCode/Roman_to_Integer.cpp b/LeetCode/Roman_to_Integer.cpp new file mode 100644 index 0000000..8d5fb48 --- /dev/null +++ b/LeetCode/Roman_to_Integer.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int romanToInt(string s) { + int result = 0; + unordered_map m { + {'I', 1}, + {'V', 5}, + {'X', 10}, + {'L', 50}, + {'C', 100}, + {'D', 500}, + {'M', 1000} + }; + + for (int i = 0; i < s.length(); i++){ + if (m[s[i]] < m[s[i + 1]]) + result -= m[s[i]]; + else + result += m[s[i]]; + } + + return result; + } +}; \ No newline at end of file