Skip to content

Commit 7a40b13

Browse files
committed
Added task 13.
1 parent 969a5c6 commit 7a40b13

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Diff for: src/main/java/s0013.roman.to.integer/Solution.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package s0013.roman.to.integer;
2+
3+
public class Solution {
4+
public int romanToInt(String s) {
5+
int x = 0;
6+
char y;
7+
for (int i = 0; i < s.length(); i++) {
8+
y = s.charAt(i);
9+
switch (y) {
10+
case 'I':
11+
if (i + 1 == s.length()) {
12+
x += 1;
13+
} else if (s.charAt(i + 1) == 'V') {
14+
x -= 1;
15+
} else if (s.charAt(i + 1) == 'X') {
16+
x -= 1;
17+
} else {
18+
x += 1;
19+
}
20+
break;
21+
case 'V':
22+
x += 5;
23+
break;
24+
case 'X':
25+
if (i + 1 == s.length()) {
26+
x += 10;
27+
} else if (s.charAt(i + 1) == 'L') {
28+
x -= 10;
29+
} else if (s.charAt(i + 1) == 'C') {
30+
x -= 10;
31+
} else {
32+
x += 10;
33+
}
34+
break;
35+
case 'L':
36+
x += 50;
37+
break;
38+
case 'C':
39+
if (i + 1 == s.length()) {
40+
x += 100;
41+
} else if (s.charAt(i + 1) == 'D') {
42+
x -= 100;
43+
} else if (s.charAt(i + 1) == 'M') {
44+
x -= 100;
45+
} else {
46+
x += 100;
47+
}
48+
break;
49+
case 'D':
50+
x += 500;
51+
break;
52+
case 'M':
53+
x += 1000;
54+
break;
55+
}
56+
}
57+
return x;
58+
}
59+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package s0013.roman.to.integer;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void romanToInt() {
11+
assertThat(new Solution().romanToInt("III"), equalTo(3));
12+
}
13+
}

0 commit comments

Comments
 (0)