-
Notifications
You must be signed in to change notification settings - Fork 2
/
RomanToInteger.java
70 lines (57 loc) · 1.54 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
package com.touir.leetcode.solutions;
import com.touir.leetcode.Solution;
public class RomanToInteger extends Solution {
/**
* problem: 13. Roman to Integer
*
* url: https://leetcode.com/problems/roman-to-integer/
*/
public int romanToInt(String s) {
int result = 0;
boolean firstIteration = true;
int lastValue = 0,
value = 0,
sum = 0;
for(char c : s.toCharArray()) {
value = romanToInt(c);
if(firstIteration) {
sum = value;
firstIteration = false;
}
else if(value == lastValue) {
sum+= value;
}
else if(value > lastValue) {
sum = value - sum;
}
else {
result += sum;
sum = value;
}
lastValue = value;
}
result += sum;
return result;
}
public int romanToInt(char c) {
switch(c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
@Override
public void test() {
System.out.println("input: X, result: " + this.romanToInt("X") + ", expected: 10");
System.out.println("input: I, result: " + this.romanToInt("I") + ", expected: 1");
System.out.println("input: III, result: " + this.romanToInt("III") + ", expected: 3");
System.out.println("input: IV, result: " + this.romanToInt("IV") + ", expected: 4");
System.out.println("input: VI, result: " + this.romanToInt("VI") + ", expected: 6");
System.out.println("input: MCMXCIV, result: " + this.romanToInt("MCMXCIV") + ", expected: 1994");
}
}