aboutsummaryrefslogtreecommitdiff
path: root/other/roman_2_int.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/roman_2_int.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/roman_2_int.cpp')
-rw-r--r--other/roman_2_int.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/other/roman_2_int.cpp b/other/roman_2_int.cpp
new file mode 100644
index 0000000..1389754
--- /dev/null
+++ b/other/roman_2_int.cpp
@@ -0,0 +1,43 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int romanToInt(string s) {
+ int ans = 0, num = 0;
+ for (int i = s.size() - 1; ~i; i--) {
+ switch (s[i]) {
+ case 'I':
+ num = 1;
+ break;
+ case 'V':
+ num = 5;
+ break;
+ case 'X':
+ num = 10;
+ break;
+ case 'L':
+ num = 50;
+ break;
+ case 'C':
+ num = 100;
+ break;
+ case 'D':
+ num = 500;
+ break;
+ case 'M':
+ num = 1000;
+ break;
+ }
+ if (4 * num < ans)
+ ans -= num;
+ else
+ ans += num;
+ }
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.romanToInt("MCMXCIVMCMXCIV");
+}