Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Roman to Integer - Leetcode 13/Roman to Integer
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int romanToInt(String s) {
Map<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=map.get(s.charAt(s.length()-1));
for(int i=s.length()-2;i>=0;i--){
if(map.get(s.charAt(i)) < map.get(s.charAt(i+1))){
result-=map.get(s.charAt(i));
}else{
result+=map.get(s.charAt(i));
}
}
return result;
}
}