We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 78f714d commit 28d51ffCopy full SHA for 28d51ff
java/0013 Roman to Integer
@@ -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