-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman_to_Integer.java
More file actions
43 lines (41 loc) · 1.23 KB
/
Roman_to_Integer.java
File metadata and controls
43 lines (41 loc) · 1.23 KB
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
34
35
36
37
38
39
40
41
42
43
package com.leet_code;
import java.util.HashMap;
public class Roman_to_Integer {
public static void main(String[] args) {
String s="MCMXCIV";
System.out.println(romanToInt(s));
}
public static int romanToInt(String s) {
HashMap<Character,Integer> hash=new HashMap<>();
hash.put('I',1);
hash.put('V',5);
hash.put('X',10);
hash.put('L',50);
hash.put('C',100);
hash.put('D',500);
hash.put('M',1000);
int sum=0;
for (int i = 0; i < s.length(); i++) {
char x=s.charAt(i);
if (i<s.length()-1 && hash.get(x)<hash.get(s.charAt(i+1))){
sum+=hash.get(s.charAt(i+1))-hash.get(x);
i++;
} else if (x=='M') {
sum+=hash.get(x);
} else if (x=='D') {
sum+=hash.get(x);
} else if (x=='C') {
sum+=hash.get(x);
} else if (x=='L') {
sum+=hash.get(x);
} else if (x=='X') {
sum+=hash.get(x);
} else if (x=='V') {
sum+=hash.get(x);
} else if (x=='I') {
sum+=hash.get(x);
}
}
return sum;
}
}