In solution like these how come compiler knows that there is need to execute program from function under public section of Class.
As upto my knowledge compiler looks for main () function in cpp to start executing function.
solution provided by rahulverma5297 ,source from leetcode
I have tried to find solution on internet sources but unable to find satisfactory solution.
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> m;
m['I'] = 1;
m['V'] = 5;
m['X'] = 10;
m['L'] = 50;
m['C'] = 100;
m['D'] = 500;
m['M'] = 1000;
int ans = 0;
for(int i = 0; i < s.length(); i++){
if(m[s[i]] < m[s[i+1]]){
ans -= m[s[i]];
}
else{
ans += m[s[i]];
}
}
return ans;
}
};
New contributor
Shivani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3