Skip to content

Commit 63f6534

Browse files
Added Solution of Roman_to_Integer in Java.
1 parent 20512c3 commit 63f6534

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Numbers/Roman_to_Integer.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)