Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int romanToInt(string s) {
- map<char,int>mp;
- mp['I'] = 1; mp['V'] = 5; mp['X'] = 10; mp['L'] = 50;
- mp['C'] = 100; mp['D'] = 500; mp['M'] = 1000;
- int val = 0;
- for(int i=0;i<s.length();i++){
- if(s[i+1] && mp[s[i]] < mp[s[i+1]]){
- val+=mp[s[i+1]] - mp[s[i]] ;
- i++;
- }
- else{
- val+=mp[s[i]];
- }
- }
- return val;
- }
- };
Add Comment
Please, Sign In to add comment