-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob13.java
More file actions
33 lines (32 loc) · 798 Bytes
/
prob13.java
File metadata and controls
33 lines (32 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public int romanToInt(String str) {
int n=str.length();
int sum=0;
HashMap<Character,Integer>map=new HashMap<>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
for(int i=0;i<n;i++){
char ch=str.charAt(i);
int val=map.get(ch);
if((i+1)<n){
int nval=map.get(str.charAt(i+1));
if(nval>val){
sum+=nval-val;
i++;
}
else{
sum+=val;
}
}
else{
sum+=val;
}
}
return sum;
}
}