Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. class Solution {
  2. public int romanToInt(String s) {
  3. HashMap<String, Integer> lookupMap = new HashMap<>();
  4. lookupMap.put("I", 1);
  5. lookupMap.put("V", 5);
  6. lookupMap.put("X", 10);
  7. lookupMap.put("L", 50);
  8. lookupMap.put("C", 100);
  9. lookupMap.put("D", 500);
  10. lookupMap.put("M", 1000);
  11.  
  12. int runningCount = 0;
  13. char previous = ' ';
  14.  
  15. for (char c : s.toCharArray()) {
  16. int currentVal = lookupMap.get(Character.toString(c));
  17. runningCount += currentVal;
  18. if ((previous == 'I')&&((c=='V')||(c=='X'))){
  19. runningCount -=2;
  20. } else if ((previous == 'X')&&((c=='L')||(c=='C'))){
  21. runningCount -= 20;
  22. } else if ((previous == 'C')&&((c=='D')||(c=='M'))){
  23. runningCount -= 200;
  24. }
  25. previous = c;
  26.  
  27. }
  28. return runningCount;
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement