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 abcff23 commit 2dcefc0Copy full SHA for 2dcefc0
Hashing/Roman_to_Integer.java
@@ -0,0 +1,33 @@
1
+class Solution {
2
+ public int romanToInt(String s) {
3
+ Map<String,Integer> map = new HashMap<String,Integer>();
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
+ int sum = 0;
12
+ for (int i = 0 ; i < s.length()-1; i++){
13
+ int temp = map.get(s.substring(i,i+1));
14
+ int next = map.get(s.substring(i+1,i+2));
15
+ if (temp < next){
16
+ sum += (next-temp);
17
+ }
18
+ else{
19
+ sum += temp;
20
21
22
+ sum += map.get(s.substring(s.length()-1));
23
+ return sum;
24
25
+}
26
+
27
+// Example Test cases
28
+// Input: s = "IV"
29
+// Output: 4
30
+//
31
32
+// Input: s = "IX"
33
+// Output: 9
0 commit comments