Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int romanToInt(String s) {
- HashMap<String, Integer> lookupMap = new HashMap<>();
- lookupMap.put("I", 1);
- lookupMap.put("V", 5);
- lookupMap.put("X", 10);
- lookupMap.put("L", 50);
- lookupMap.put("C", 100);
- lookupMap.put("D", 500);
- lookupMap.put("M", 1000);
- int runningCount = 0;
- char previous = ' ';
- for (char c : s.toCharArray()) {
- int currentVal = lookupMap.get(Character.toString(c));
- runningCount += currentVal;
- if ((previous == 'I')&&((c=='V')||(c=='X'))){
- runningCount -=2;
- } else if ((previous == 'X')&&((c=='L')||(c=='C'))){
- runningCount -= 20;
- } else if ((previous == 'C')&&((c=='D')||(c=='M'))){
- runningCount -= 200;
- }
- previous = c;
- }
- return runningCount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement