We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fe1c9ac commit 152332eCopy full SHA for 152332e
1 file changed
LeetCode/romanToInt.cpp
@@ -0,0 +1,36 @@
1
+#include <iostream>
2
+#include <map>
3
+#include <string>
4
+using namespace std;
5
+
6
+#define REP(i,n) for(int i=0;i<(n);++i)
7
+#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
8
+#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
9
10
+class Solution {
11
+public:
12
+ int romanToInt(string s) {
13
+ map<char, int> mp;
14
+ mp['I'] = 1;
15
+ mp['V'] = 5;
16
+ mp['X'] = 10;
17
+ mp['L'] = 50;
18
+ mp['C'] = 100;
19
+ mp['D'] = 500;
20
+ mp['M'] = 1000;
21
22
+ int n = s.length();
23
+ int ret = mp[s[n - 1]];
24
+ RFOR(i,n-2,0) {
25
+ if (mp[s[i]] >= mp[s[i + 1]]) ret += mp[s[i]];
26
+ else ret -= mp[s[i]];
27
+ }
28
+ return ret;
29
30
+};
31
32
+int main() {
33
+ Solution s = Solution();
34
+ cout << s.romanToInt("MMCDLVI") << endl;
35
+ return 0;
36
+}
0 commit comments