-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInt_13.java
More file actions
87 lines (81 loc) · 2.44 KB
/
Copy pathRomanToInt_13.java
File metadata and controls
87 lines (81 loc) · 2.44 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package Design;
import java.util.HashMap;
import java.util.Map;
public class RomanToInt_13 {
public int romanToIntFast(String s) {
Map<Character,Integer> rule=new HashMap<Character, Integer>(){
{
put('M',1000);
put('D',500);
put('C',100);
put('L',50);
put('X',10);
put('V',5);
put('I',1);
}
};
/**
* 通常情况,罗马数字中小的数字位于大的数字右侧,如果出现右侧数字比当前大,需要减去当前罗马字符所代表的数字
*/
int sum=0;
for(int i=0;i<s.length();i++){
// char c=s.charAt(i);
// char c2=i+1<s.length()?s.charAt(i+1):'0';
// if('0'!=c2&&rule.get(c)<rule.get(c2)){
// sum-=rule.get(c);
// }else{
// sum+=rule.get(c);
// }
int value=rule.get(s.charAt(i));
if(i<s.length()-1&&value<rule.get(s.charAt(i+1))){
sum-=value;
}else{
sum+=value;
}
}
return sum;
}
public int romanToInt(String s) {
Map<String,Integer> relation=new HashMap<String, Integer>(){
{
put("M",1000);
put("CM",900);
put("D",500);
put("CD",400);
put("C",100);
put("XC",90);
put("L",50);
put("XL",40);
put("X",10);
put("IX",9);
put("V",5);
put("IV",4);
put("I",1);
}
};
char[] str=s.toCharArray();
int sum=0;
for(int i=0;i<str.length;){
if(i+1<str.length){
String s1 = new String(str, i, 2);
if(relation.containsKey(s1)){
sum+=relation.get(s1);
i+=2;
continue;
}
}
sum+=relation.get(str[i]+"");
i++;
}
return sum;
}
public static void main(String[] args) {
RomanToInt_13 test = new RomanToInt_13();
int s1 = test.romanToIntFast("LVIII");
int s2 = test.romanToIntFast("III");
int s3 = test.romanToIntFast("IX");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}