Skip to content

Commit 6aa5186

Browse files
[N-0] refactor 13
1 parent a1f5dd7 commit 6aa5186

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ Your ideas/fixes/algorithms are more than welcome!
654654
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_16.java)|O(nlogn)|O(1)|Medium|Two Pointers
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
657-
|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
657+
|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 | Math, String
658658
|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

src/main/java/com/fishercoder/solutions/_13.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@
1212

1313
public class _13 {
1414

15-
public int romanToInt(String s) {
16-
Map<Character, Integer> map = new HashMap();
17-
map.put('I', 1);
18-
map.put('V', 5);
19-
map.put('X', 10);
20-
map.put('L', 50);
21-
map.put('C', 100);
22-
map.put('D', 500);
23-
map.put('M', 1000);
15+
public static class Solution1 {
16+
public int romanToInt(String s) {
17+
Map<Character, Integer> map = new HashMap();
18+
map.put('I', 1);
19+
map.put('V', 5);
20+
map.put('X', 10);
21+
map.put('L', 50);
22+
map.put('C', 100);
23+
map.put('D', 500);
24+
map.put('M', 1000);
2425

25-
char[] schar = s.toCharArray();
26-
int result = 0;
27-
for (int i = 0; i < s.length(); i++) {
28-
if (i > 0 && map.get(schar[i]) > map.get(schar[i - 1])) {
29-
result = result + map.get(schar[i]) - 2 * map.get(schar[i - 1]);
30-
} else {
31-
result = result + map.get(schar[i]);
26+
int result = 0;
27+
for (int i = 0; i < s.length(); i++) {
28+
if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i - 1))) {
29+
result += map.get(s.charAt(i)) - 2 * map.get(s.charAt(i - 1));
30+
} else {
31+
result += map.get(s.charAt(i));
32+
}
3233
}
34+
return result;
3335
}
34-
return result;
3536
}
3637

3738
}
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._13;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _13Test {
10+
private static _13.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _13.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(12, solution1.romanToInt("XII"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1000, solution1.romanToInt("M"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(3999, solution1.romanToInt("MMMCMXCIX"));
30+
}
31+
}

0 commit comments

Comments
 (0)