Skip to content

Commit 17d6518

Browse files
Merge pull request #112 from stubborn-akshat/patch-2
Roman_to_Integer.java
2 parents c0b75a3 + 5ac3e02 commit 17d6518

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

13. Roman to Integer

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int romanToInt(String s) {
3+
4+
HashMap<Character,Integer> map=new HashMap<>();
5+
map.put('I',1);
6+
map.put('V',5);
7+
map.put('X',10);
8+
map.put('L',50);
9+
map.put('C',100);
10+
map.put('D',500);
11+
map.put('M',1000);
12+
13+
int sum=0;
14+
for(int i=0;i<s.length();i++){
15+
char ch=s.charAt(i);
16+
int a=map.get(ch);
17+
18+
if(i+1<s.length()){
19+
int b=map.get(s.charAt(i+1));
20+
21+
if(a>=b){
22+
sum=sum+a;
23+
}
24+
else{
25+
sum=sum-a;
26+
}
27+
}
28+
else{
29+
sum=sum+a;
30+
}
31+
}
32+
return sum;
33+
}
34+
}

0 commit comments

Comments
 (0)