Skip to content

Commit 9161e0b

Browse files
authored
Merge pull request #2287 from n0nchalant/patch-2
java/0013-roman-to-integer
2 parents 919331c + ec0d31b commit 9161e0b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: java/0013-roman-to-integer.java

+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)