-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInteger.java
More file actions
57 lines (48 loc) · 1.33 KB
/
Copy pathRomanToInteger.java
File metadata and controls
57 lines (48 loc) · 1.33 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public int romanToInt(String s) {
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);
if(s.equals("IV")){
return 4;
}
if(s.equals("IX")){
return 9;
}
if(s.equals("XL")){
return 40;
}
if(s.equals("XC")){
return 90;
}if(s.equals("CD")){
return 400;
}
if(s.equals("CM")){
return 900;
}
System.out.println(s.length());
int integerVal = 0;
int temp = 0;
for(int i=0;i<s.length();i++){
if(i==(s.length()-1)){
integerVal = integerVal+map.get(s.charAt(i));
break;
}
if(map.get(s.charAt(i)) >= map.get(s.charAt(i+1))){
integerVal = integerVal + map.get(s.charAt(i));
}
else{
temp = map.get(s.charAt(i+1)) - map.get(s.charAt(i));
integerVal = integerVal + temp;
temp = 0;
i = i + 1;
}
}
return integerVal;
}
}