-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanToInteger.java
95 lines (79 loc) · 2.43 KB
/
RomanToInteger.java
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
88
89
90
91
92
93
94
95
/*
罗马数字变成整形
如果后面的数字比前面的大,则视为减
http://blog.csdn.net/wzy_1988/article/details/17057929
*/
public class Solution {
public int romanToInt(String s) {
if(s == null || s.length() == 0) return 0;
int i, pre, curr, sum;
sum = RomanTable(s.charAt(0));
for(i = 1; i < s.length(); i++){
pre = RomanTable(s.charAt(i-1));
curr = RomanTable(s.charAt(i));
if(curr <= pre){
sum += curr;
}else{
sum = sum + curr - 2*pre;
}
}
return sum;
}
//这里的逻辑是从前往后,当前的数是+还是- 取决去后面的数
// 在for循环里,,
//判断后面一位的时候,都是加,所以当i已经到了尾巴,直接加上,为了不越界
/*
public int romanToInt(String s) {
if(s == null || s.length() == 0) return 0;
char[] arr = s.toCharArray();
int sum = 0, curr;
for(int i = 0; i < arr.length; i++){
curr = RomanTable(arr[i]);
if(i == arr.length - 1 || RomanTable(arr[i + 1])<= curr ){
sum += curr;
}else{
sum -= curr;
}
}
return sum;
}
*/
public int RomanTable(char c){
int num = 0;
switch(c){
case 'I': num = 1; break;
case 'V': num = 5; break;
case 'X': num = 10; break;
case 'L': num = 50; break;
case 'C': num = 100; break;
case 'D': num = 500; break;
case 'M': num = 1000; break;
}
return num;
}
}
// solution 2
public class Solution {
public int romanToInt(String s) {
Map<Character, Integer> romans = new HashMap<Character, Integer>();
romans.put('I', 1);
romans.put('V', 5);
romans.put('X', 10);
romans.put('L', 50);
romans.put('C', 100);
romans.put('D', 500);
romans.put('M', 1000);
char[] cs = s.toCharArray();
int num = 0;
int val;
for (int i = 0; i < cs.length; i++) {
val = romans.get(cs[i]);
if (i == cs.length - 1 || romans.get(cs[i + 1]) <= val) {
num += val;
} else {
num -= val;
}
}
return num;
}
}