Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ Welcome to the list of people who contributed to this repo 💥
7. [Ayansh](https://github.com/badasschef)
8. [Deep](https://github.com/deep846)
9. [Amit](https://github.com/amitShindeGit)
10. [Pulkit](https://github.com/practicalmetal)

41 changes: 41 additions & 0 deletions Math/Roman_To_Integer/CPP/Roman_To_Integer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:
int romanToInt(string s) {
int roman[256],sum=0;
roman['I']=1;
roman['V']=5;
roman['X']=10;
roman['L']=50;
roman['C']=100;
roman['D']=500;
roman['M']=1000;

int i=0;
while(i<s.size())
{
int countOfDuplicate=1;
while(s[i]==s[i+1])
{
countOfDuplicate++;
i++;
}
if(i==s.size()-1)
{
sum+=countOfDuplicate*roman[s[i]];
return sum;
}
else if(roman[s[i]]<roman[s[i+1]])
{
sum+=roman[s[i+1]]-(countOfDuplicate*roman[s[i]]);
i+=2;
}
else
{
sum+=(countOfDuplicate*roman[s[i]]);
i+=1;
}

}
return sum;
}
};