We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 600bb08 commit 447a272Copy full SHA for 447a272
EASY/src/easy/RomantoInteger.java
@@ -0,0 +1,30 @@
1
+package easy;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
6
+public class RomantoInteger {
7
8
+ public int romanToInt(String s) {
9
+ Map<Character, Integer> map = new HashMap();
10
+ map.put('I', 1);
11
+ map.put('V', 5);
12
+ map.put('X', 10);
13
+ map.put('L', 50);
14
+ map.put('C', 100);
15
+ map.put('D', 500);
16
+ map.put('M', 1000);
17
18
+ char[] schar = s.toCharArray();
19
+ int result = 0;
20
+ for(int i = 0; i < s.length(); i++){
21
+ if(i > 0 && map.get(schar[i]) > map.get(schar[i-1])){
22
+ result = result + map.get(schar[i]) - 2*map.get(schar[i-1]);
23
+ } else {
24
+ result = result + map.get(schar[i]);
25
+ }
26
27
+ return result;
28
29
30
+}
0 commit comments