From 57e334cf8ba999e6f1bb291fd9aa827ae4a89aa4 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 23 Feb 2025 01:58:39 +0000 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20#454=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=86=94=EB=A3=A8=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeetCode/Roman_to_Integer.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 LeetCode/Roman_to_Integer.cpp 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