mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
Leetcode day 2 and 3
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
class Solution{
|
||||
public:
|
||||
int romanToInt(string s){
|
||||
unordered_map<char,int> convert = {{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
|
||||
int result = 0;
|
||||
for(int i=0;i<s.size();++i){
|
||||
int curr = convert[s[i]];
|
||||
int next = ((i+1) < s.size())? convert[s[i+1]] : 0;
|
||||
if(curr<next){
|
||||
result-=curr;
|
||||
}else{
|
||||
result+=curr;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user