Skip to content

Commit b1f92a8

Browse files
authored
Added solutions 12. & 13., Int to Roman & Roman to Int (#76)
* Added solutions for Integer to Roman (q. 12) and Roman to Integer (q. 13) * Updated README.md * Removed main methods
1 parent f314454 commit b1f92a8

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

Java/integer-to-roman.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
public class IntegerToRoman{
2+
public static String numberToRoman(int number){//Converts a number into a roman numerial
3+
String roman = "";
4+
while(number != 0){//checks if the number is not equal to 0, if it is not equal then there is more processing to be done
5+
if(number >= 1000){//if the number is larger than 1000, then add an M to the roman string and subtract 1000 from number
6+
roman += "M";
7+
number -= 1000;
8+
}
9+
else if(number >= 900){//if the number is larger than 900, then add an CM to the roman string and subtract 900 from number
10+
roman += "CM";
11+
number -= 900;
12+
}
13+
else if(number >= 500){//if the number is larger than 500, then add an D to the roman string and subtract 500 from number
14+
roman += "D";
15+
number -= 500;
16+
}
17+
else if(number >= 400){//if the number is larger than 400, then add an CD to the roman string and subtract 400 from number
18+
roman += "CD";
19+
number -= 400;
20+
}
21+
else if(number >= 100){//if the number is larger than 100, then add an C to the roman string and subtract 100 from number
22+
roman += "C";
23+
number -= 100;
24+
}
25+
else if(number >= 90){//if the number is larger than 90, then add an XC to the roman string and subtract 90 from number
26+
roman += "XC";
27+
number -= 90;
28+
}
29+
else if(number >= 50){//if the number is larger than 50, then add an L to the roman string and subtract 50 from number
30+
roman += "L";
31+
number -= 50;
32+
}
33+
else if(number >= 40){//if the number is larger than 40, then add an XL to the roman string and subtract 40 from number
34+
roman += "XL";
35+
number -= 40;
36+
}
37+
else if(number >= 10){//if the number is larger than 10, then add an X to the roman string and subtract 10 from number
38+
roman += "X";
39+
number -= 10;
40+
}
41+
else if(number >= 9){//if the number is larger than 9, then add an IX to the roman string and subtract 9 from number
42+
roman += "IX";
43+
number -= 9;
44+
}
45+
else if(number >= 5){//if the number is larger than 5, then add an V to the roman string and subtract 5 from number
46+
roman += "V";
47+
number -= 5;
48+
}
49+
else if(number >= 4){//if the number is larger than 4, then add an IV to the roman string and subtract 4 from number
50+
roman += "IV";
51+
number -= 4;
52+
}
53+
else if(number >= 1){//if the number is larger than 1, then add an I to the roman string and subtract 1 from number
54+
roman += "I";
55+
number -= 1;
56+
}
57+
}
58+
return roman;
59+
}
60+
}

Java/roman-to-integer.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class RomanToInteger{
2+
public static int GetNum(int num) {//gets the number assoicate with a roman character
3+
if (num == 'I' || num == 'i') {
4+
return 1;
5+
}
6+
if (num == 'V' || num == 'v') {
7+
return 5;
8+
}
9+
if (num == 'X' || num == 'x') {
10+
return 10;
11+
}
12+
if (num == 'L' || num == 'l') {
13+
return 50;
14+
}
15+
if (num == 'C' || num == 'c') {
16+
return 100;
17+
}
18+
if (num == 'D' || num == 'd') {
19+
return 500;
20+
}
21+
if (num == 'M' || num == 'm') {
22+
return 1000;
23+
}
24+
25+
return -1;
26+
}
27+
28+
public static int romanToNumber(String roman){//Converts a roman numerial into a number
29+
int results = 0;
30+
for (int i = 0; i < roman.length(); i++) {//iterate through all the characters
31+
int cur = GetNum(roman.charAt(i));//Gets the integer of the roman number
32+
if (cur == -1) {//checks if the cur num is -1, if it is -1, it means that it is an invalid character
33+
return -1;
34+
}
35+
if (i + 1 < roman.length()) {//checks if the next index is out of bounds
36+
int next = GetNum(roman.charAt(i + 1));//gets the next character character
37+
if (next == -1) {//checks if the cur num is -1, if it is -1, it means that it is an invalid character
38+
return -1;
39+
}
40+
if (cur >= next) {//compares the current number to the next number
41+
results += cur;//if it is bigger, simply add the current number
42+
}
43+
else {//if the current number is smaller than the next number
44+
results += (next - cur);//subtract the next number by the current number
45+
i++;//increment the index, since the next number has already been used
46+
}
47+
}
48+
else {//only here to add the last number to the results
49+
results += cur;//adds the last character to the results
50+
}
51+
}
52+
return results;//returns the result
53+
}
54+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
246246
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | [C++](./C++/Excel-Sheet-Column-Title.cpp) | _O(n)_ | _O(n)_ | Easy | String | |
247247
| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java) <br> [C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | |
248248
| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | |
249-
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | |
249+
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | |
250+
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | |
251+
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) | _O(n)_ | _O(1)_ | Easy | Math | |
250252
<br/>
251253
<div align="right">
252254
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)