Guest User

Untitled

a guest
Apr 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //I V X L C D M
  5. //1 5 10 50 100 500 1000
  6.  
  7. unordered_map<char, int> convert = {
  8. {'I', 1},
  9. {'V', 5},
  10. {'X', 10},
  11. {'L', 50},
  12. {'C', 100},
  13. {'D', 500},
  14. {'M', 1000}
  15. };
  16.  
  17. class Solution {
  18. public:
  19. int romanToInt(string s) {
  20. int ans = 0;
  21. for(int i = 0 ; i < (int)s.length() ; i++){
  22. if(i != (int)s.length() - 1 && convert[s[i]] < convert[s[i + 1]])
  23. ans -= convert[s[i]];
  24. else
  25. ans += convert[s[i]];
  26. }
  27. return ans;
  28. }
  29. };
  30.  
  31. int main(){
  32. return 0;
  33. }
Add Comment
Please, Sign In to add comment