Skip to content

Commit

Permalink
Create 0013 Roman to Integer
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nchalant authored Feb 24, 2023
1 parent 78f714d commit 28d51ff
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions java/0013 Roman to Integer
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int romanToInt(String s) {
HashMap<Character, Integer> map = new HashMap();
map.put('I', 1);
map.put('V' ,5);
map.put('X' ,10);
map.put('L' ,50);
map.put('C' ,100);
map.put('D' ,500);
map.put('M' ,1000);

int result = 0;
for(int i=0; i < s.length(); i++){
if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i-1))) {
result += map.get(s.charAt(i)) - 2*map.get(s.charAt(i-1));
} else {
result += map.get(s.charAt(i));
}
}
return result;
}
}

0 comments on commit 28d51ff

Please sign in to comment.