From 867939dfae70cad292a759a44cf6de558b9179a2 Mon Sep 17 00:00:00 2001 From: Alan Tsui Date: Wed, 7 Oct 2020 00:09:00 -0400 Subject: [PATCH 1/3] Added solutions for Integer to Roman (q. 12) and Roman to Integer (q. 13) --- Java/integer-to-roman.java | 64 ++++++++++++++++++++++++++++++++++++++ Java/roman-to-integer.java | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 Java/integer-to-roman.java create mode 100644 Java/roman-to-integer.java diff --git a/Java/integer-to-roman.java b/Java/integer-to-roman.java new file mode 100644 index 00000000..00a13d1b --- /dev/null +++ b/Java/integer-to-roman.java @@ -0,0 +1,64 @@ +public class IntegerToRoman{ + public static String numberToRoman(int number){//Converts a number into a roman numerial + String roman = ""; + 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 + if(number >= 1000){//if the number is larger than 1000, then add an M to the roman string and subtract 1000 from number + roman += "M"; + number -= 1000; + } + else if(number >= 900){//if the number is larger than 900, then add an CM to the roman string and subtract 900 from number + roman += "CM"; + number -= 900; + } + else if(number >= 500){//if the number is larger than 500, then add an D to the roman string and subtract 500 from number + roman += "D"; + number -= 500; + } + else if(number >= 400){//if the number is larger than 400, then add an CD to the roman string and subtract 400 from number + roman += "CD"; + number -= 400; + } + else if(number >= 100){//if the number is larger than 100, then add an C to the roman string and subtract 100 from number + roman += "C"; + number -= 100; + } + else if(number >= 90){//if the number is larger than 90, then add an XC to the roman string and subtract 90 from number + roman += "XC"; + number -= 90; + } + else if(number >= 50){//if the number is larger than 50, then add an L to the roman string and subtract 50 from number + roman += "L"; + number -= 50; + } + else if(number >= 40){//if the number is larger than 40, then add an XL to the roman string and subtract 40 from number + roman += "XL"; + number -= 40; + } + else if(number >= 10){//if the number is larger than 10, then add an X to the roman string and subtract 10 from number + roman += "X"; + number -= 10; + } + else if(number >= 9){//if the number is larger than 9, then add an IX to the roman string and subtract 9 from number + roman += "IX"; + number -= 9; + } + else if(number >= 5){//if the number is larger than 5, then add an V to the roman string and subtract 5 from number + roman += "V"; + number -= 5; + } + else if(number >= 4){//if the number is larger than 4, then add an IV to the roman string and subtract 4 from number + roman += "IV"; + number -= 4; + } + else if(number >= 1){//if the number is larger than 1, then add an I to the roman string and subtract 1 from number + roman += "I"; + number -= 1; + } + } + return roman; + } + + public static void main(String[] args){ + System.out.println(numberToRoman(123));//123 will return CXXIII + } +} \ No newline at end of file diff --git a/Java/roman-to-integer.java b/Java/roman-to-integer.java new file mode 100644 index 00000000..c258bd00 --- /dev/null +++ b/Java/roman-to-integer.java @@ -0,0 +1,58 @@ +public class RomanToInteger{ + public static int GetNum(int num) {//gets the number assoicate with a roman character + if (num == 'I' || num == 'i') { + return 1; + } + if (num == 'V' || num == 'v') { + return 5; + } + if (num == 'X' || num == 'x') { + return 10; + } + if (num == 'L' || num == 'l') { + return 50; + } + if (num == 'C' || num == 'c') { + return 100; + } + if (num == 'D' || num == 'd') { + return 500; + } + if (num == 'M' || num == 'm') { + return 1000; + } + + return -1; + } + + public static int romanToNumber(String roman){//Converts a roman numerial into a number + int results = 0; + for (int i = 0; i < roman.length(); i++) {//iterate through all the characters + int cur = GetNum(roman.charAt(i));//Gets the integer of the roman number + if (cur == -1) {//checks if the cur num is -1, if it is -1, it means that it is an invalid character + return -1; + } + if (i + 1 < roman.length()) {//checks if the next index is out of bounds + int next = GetNum(roman.charAt(i + 1));//gets the next character character + if (next == -1) {//checks if the cur num is -1, if it is -1, it means that it is an invalid character + return -1; + } + if (cur >= next) {//compares the current number to the next number + results += cur;//if it is bigger, simply add the current number + } + else {//if the current number is smaller than the next number + results += (next - cur);//subtract the next number by the current number + i++;//increment the index, since the next number has already been used + } + } + else {//only here to add the last number to the results + results += cur;//adds the last character to the results + } + } + return results;//returns the result + } + + public static void main(String[] args) { + System.out.println(romanToNumber("VI"));//VI will return 6 + } +} \ No newline at end of file From 9c9cbfec8fd3049bf15981cfae45c6081dcadf44 Mon Sep 17 00:00:00 2001 From: Alan Tsui Date: Wed, 7 Oct 2020 00:27:24 -0400 Subject: [PATCH 2/3] Updated README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 358c2d0d..de525959 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | | | 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java)
[C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | | | 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | | -| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | | +| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | | +| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | | +| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) | _O(n)_ | _O(1)_ | Easy | Math | |
⬆️ Back to Top From 71bae7581337c0595a2819bd64e68cba1afdd424 Mon Sep 17 00:00:00 2001 From: Alan Tsui Date: Wed, 7 Oct 2020 00:53:03 -0400 Subject: [PATCH 3/3] Removed main methods --- Java/integer-to-roman.java | 4 ---- Java/roman-to-integer.java | 4 ---- 2 files changed, 8 deletions(-) diff --git a/Java/integer-to-roman.java b/Java/integer-to-roman.java index 00a13d1b..066721b1 100644 --- a/Java/integer-to-roman.java +++ b/Java/integer-to-roman.java @@ -57,8 +57,4 @@ else if(number >= 1){//if the number is larger than 1, then add an I to the roma } return roman; } - - public static void main(String[] args){ - System.out.println(numberToRoman(123));//123 will return CXXIII - } } \ No newline at end of file diff --git a/Java/roman-to-integer.java b/Java/roman-to-integer.java index c258bd00..47c12634 100644 --- a/Java/roman-to-integer.java +++ b/Java/roman-to-integer.java @@ -51,8 +51,4 @@ public static int romanToNumber(String roman){//Converts a roman numerial into a } return results;//returns the result } - - public static void main(String[] args) { - System.out.println(romanToNumber("VI"));//VI will return 6 - } } \ No newline at end of file