Skip to content

Commit 28d51ff

Browse files
authored
Create 0013 Roman to Integer
1 parent 78f714d commit 28d51ff

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: java/0013 Roman to Integer

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int romanToInt(String s) {
3+
HashMap<Character, Integer> map = new HashMap();
4+
map.put('I', 1);
5+
map.put('V' ,5);
6+
map.put('X' ,10);
7+
map.put('L' ,50);
8+
map.put('C' ,100);
9+
map.put('D' ,500);
10+
map.put('M' ,1000);
11+
12+
int result = 0;
13+
for(int i=0; i < s.length(); i++){
14+
if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i-1))) {
15+
result += map.get(s.charAt(i)) - 2*map.get(s.charAt(i-1));
16+
} else {
17+
result += map.get(s.charAt(i));
18+
}
19+
}
20+
return result;
21+
}
22+
}

0 commit comments

Comments
 (0)