Skip to content

Commit b0ce2c1

Browse files
committed
Added solution for Roman To Integer
1 parent f2d4d66 commit b0ce2c1

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package problems.leetcode.hash_table;
2+
3+
public class RomanToInteger {
4+
public int romanToInt(String s) {
5+
int result = 0, previous = 0, current = 0;
6+
char[] chars = s.toCharArray();
7+
for (char romanChar : chars) {
8+
switch (romanChar) {
9+
case 'I' -> current = 1;
10+
case 'V' -> current = 5;
11+
case 'X' -> current = 10;
12+
case 'L' -> current = 50;
13+
case 'C' -> current = 100;
14+
case 'D' -> current = 500;
15+
case 'M' -> current = 1000;
16+
}
17+
if (previous < current)
18+
// because we add previous once before and need to remove that
19+
result += current - 2 * previous;
20+
else
21+
result += current;
22+
previous = current;
23+
}
24+
return result;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package problems.leetcode.hash_table;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class RomanToIntegerTest {
9+
10+
@Test
11+
void testSimple() {
12+
RomanToInteger romanToInteger = new RomanToInteger();
13+
String input = "III";
14+
int result = romanToInteger.romanToInt(input);
15+
assertEquals(3, result);
16+
}
17+
18+
@Test
19+
void testMultipleSymbols() {
20+
RomanToInteger romanToInteger = new RomanToInteger();
21+
String input = "MDCLXVI";
22+
int result = romanToInteger.romanToInt(input);
23+
assertEquals(1666, result);
24+
}
25+
26+
@Test
27+
void testComplexString() {
28+
RomanToInteger romanToInteger = new RomanToInteger();
29+
String input = "MCMXCIV";
30+
int result = romanToInteger.romanToInt(input);
31+
assertEquals(1994, result);
32+
}
33+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This repository contains my solutions to various LeetCode problems. The solutions are implemented in both Java and JavaScript. The Java solutions are located in the `Java/src/` directory, while the JavaScript solutions are divided into problem implementations in `js/Problems/` and corresponding Jest test cases in `js/Tests/`.
44

55
## Links to my profiles
6+
67
- [Project Euler](https://projecteuler.net/progress=PalmaAnd)
78
- [LeetCode](https://leetcode.com/PalmaAnd/)
89

@@ -41,6 +42,7 @@ Here is a list of the problems I've solved along with links to the corresponding
4142
| [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Java](Java/src/main/java/problems/leetcode/stack/ReversePolish.java) |
4243
| [3Sum](https://leetcode.com/problems/3sum/) | [Java](Java/src/main/java/problems/leetcode/two_pointers/ThreeSum.java) |
4344
| [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix) | [Java](Java/src/main/java/problems/leetcode/others/LongestCommonPrefix.java) |
45+
| [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](Java/src/main/java/problems/leetcode/hash_table/RomanToInteger.java) |
4446

4547
## Java Solutions
4648

0 commit comments

Comments
 (0)