-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13. Roman to Integer.cpp
More file actions
53 lines (48 loc) · 1.12 KB
/
13. Roman to Integer.cpp
File metadata and controls
53 lines (48 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
class Solution {
public:
int romanToInt(string s) {
map<string,int> m{
{"M",1000},
{"CM",900},
{"D",500},
{"CD",400},
{"C",100},
{"XC",90},
{"L",50},
{"XL",40},
{"X",10},
{"IX",9},
{"V",5},
{"IV",4},
{"I",1}
};
int ans=0;
int len=s.length();
for(int i=0;i<len;){
// get current string
string st="";
st+=s[i];
// if specials possible make them
// eg 4 =>IV
int flag=0;
if(i+1<len){
if(m[st+s[i+1]]!=0){
flag=1;
ans+=m[st+s[i+1]];
i+=2;
}
}
// if special not possible
if(flag==0){
if(m[st]!=0){
ans+=m[st];
i+=1;
}
}
}
return ans;
}
};