-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanToInteger.java
52 lines (50 loc) · 1.38 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
package com.leetcode;
/**
* Created by jamylu on 2017/12/31.
* leetcode013.
* 罗马数字转化为整型,主要是理解罗马数字表示的含义
*/
public class RomanToInteger {
public static void main(String[] args) {
System.out.println(romanToInt("CDIV"));
}
public static int romanToInt(String s) {
int res = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
switch (c) {
case 'I':
if (res >= 5)
res += -1;
else
res += 1;
break;
case 'V':
res += 5;
break;
case 'X':
if (res >= 50)
res += -10;
else
res += 10;
break;
case 'L':
res += 50;
break;
case 'C':
if (res >= 500)
res += -100;
else
res += 100;
break;
case 'D':
res += 500;
break;
case 'M':
res += 1000;
break;
}
}
return res;
}
}