Skip to content

Commit a1f5dd7

Browse files
[N-0] refactor 12
1 parent a7f1b3c commit a1f5dd7

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ Your ideas/fixes/algorithms are more than welcome!
655655
|15|[3Sum](https://leetcode.com/problems/3sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_15.java)|O(n^2)|O(1)|Medium|Two Pointers, Binary Search
656656
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_14.java)| O(n*min(wordLength in this array)) | O(1) | Easy
657657
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer)|[Solution](../master/src/main/java/com/fishercoder/solutions/_13.java)| O(1) | O(1) | Easy
658-
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_12.java)|O(1)|O(1)|Medium|
658+
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_12.java)|O(1)|O(1)|Medium| Math, String
659659
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_11.java)|O(n)|O(1)|Medium|
660660
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_10.java)|O(m*n)|O(m*n)|Hard|DP
661661
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_9.java)| O(n) | O(1) | Easy
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 12. Integer to Roman
5+
*
46
* Given an integer, convert it to a roman numeral.
57
* Input is guaranteed to be within the range from 1 to 3999.
68
*/
79
public class _12 {
8-
//looked at this post: https://discuss.leetcode.com/topic/12384/simple-solution
9-
public String intToRoman(int num) {
10-
String[] M = new String[]{"", "m", "MM", "MMM"};
11-
String[] C = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
12-
String[] X = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
13-
String[] I = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
14-
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
10+
11+
public static class Solution1 {
12+
public String intToRoman(int num) {
13+
String[] M = new String[]{"", "M", "MM", "MMM"};
14+
String[] C = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
15+
String[] X = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
16+
String[] I = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
17+
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
18+
}
1519
}
1620

1721
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._12;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _12Test {
10+
private static _12.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _12.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("XII", solution1.intToRoman(12));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("M", solution1.intToRoman(1000));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("MMMCMXCIX", solution1.intToRoman(3999));
30+
}
31+
}

0 commit comments

Comments
 (0)