Skip to content

Commit f00772c

Browse files
refactor 93
1 parent e8f7f3f commit f00772c

File tree

3 files changed

+58
-60
lines changed

3 files changed

+58
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ Your ideas/fixes/algorithms are more than welcome!
596596
|96|[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_96.java) | O(n^2) | O(n) | |Medium | Recursion, DP
597597
|95|[Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_95.java) | O(?) | O(?) | |Medium | Recursion
598598
|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_94.java)| O(n)|O(h) | |Medium| Binary Tree
599-
|93|[Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_93.java)| O(1)|O(1) | |Medium | Backtracking
599+
|93|[Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_93.java)| O(?)|O(?) | |Medium | Backtracking
600600
|92|[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_92.java)| O(n)|O(1) | |Medium
601601
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_91.java)| O(n)|O(n) | |Medium| DP
602602
|90|[Subsets II](https://leetcode.com/problems/subsets-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_90.java)|O(n^2) |O(1)||Medium|Backtracking

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

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,57 @@
66
/**
77
* 93. Restore IP Addresses
88
*
9-
* Given a string containing only digits, restore it by returning all possible valid IP address combinations.
10-
11-
For example:
12-
Given "25525511135",
13-
14-
return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
9+
* Given a string containing only digits, restore it by returning all possible valid IP address
10+
* combinations.
11+
*
12+
* For example: Given "25525511135",
13+
*
14+
* return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
1515
*/
1616
public class _93 {
1717

18+
public static class Solution1 {
1819
public List<String> restoreIpAddresses(String s) {
19-
List<String> allValidIpAddresses = new ArrayList<>();
20-
if (s == null || s.length() > 12 || s.length() < 4) {
21-
return allValidIpAddresses;
22-
}
23-
backtracking(s, new ArrayList<>(), allValidIpAddresses, 0);
20+
List<String> allValidIpAddresses = new ArrayList<>();
21+
if (s == null || s.length() > 12 || s.length() < 4) {
2422
return allValidIpAddresses;
23+
}
24+
backtracking(s, new ArrayList<>(), allValidIpAddresses, 0);
25+
return allValidIpAddresses;
2526
}
2627

2728
private void backtracking(String s, ArrayList<String> bytes, List<String> result, int pos) {
28-
if (bytes.size() == 4) {
29-
if (pos != s.length()) {
30-
return;
31-
}
32-
StringBuilder stringBuilder = new StringBuilder();
33-
for (int i = 0; i < 4; i++) {
34-
stringBuilder.append(bytes.get(i));
35-
stringBuilder.append(".");
36-
}
37-
stringBuilder.setLength(stringBuilder.length() - 1);
38-
result.add(stringBuilder.toString());
39-
return;
29+
if (bytes.size() == 4) {
30+
if (pos != s.length()) {
31+
return;
4032
}
41-
42-
for (int i = pos; i < pos + 4 && i < s.length(); i++) {
43-
String oneByte = s.substring(pos, i + 1);
44-
if (!isValid(oneByte)) {
45-
continue;
46-
}
47-
bytes.add(oneByte);
48-
backtracking(s, bytes, result, i + 1);
49-
bytes.remove(bytes.size() - 1);
33+
StringBuilder stringBuilder = new StringBuilder();
34+
for (int i = 0; i < 4; i++) {
35+
stringBuilder.append(bytes.get(i));
36+
stringBuilder.append(".");
37+
}
38+
stringBuilder.setLength(stringBuilder.length() - 1);
39+
result.add(stringBuilder.toString());
40+
return;
41+
}
42+
43+
for (int i = pos; i < pos + 4 && i < s.length(); i++) {
44+
String oneByte = s.substring(pos, i + 1);
45+
if (!isValid(oneByte)) {
46+
continue;
5047
}
48+
bytes.add(oneByte);
49+
backtracking(s, bytes, result, i + 1);
50+
bytes.remove(bytes.size() - 1);
51+
}
5152
}
5253

5354
private boolean isValid(String oneByte) {
54-
if (oneByte.charAt(0) == '0') {
55-
return oneByte.equals("0");
56-
}
57-
int num = Integer.valueOf(oneByte);
58-
return (num >= 0 && num < 256);
55+
if (oneByte.charAt(0) == '0') {
56+
return oneByte.equals("0");
57+
}
58+
int num = Integer.valueOf(oneByte);
59+
return (num >= 0 && num < 256);
5960
}
60-
61+
}
6162
}

src/test/java/com/fishercoder/_93Test.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,25 @@
1111
import static junit.framework.Assert.assertEquals;
1212

1313
public class _93Test {
14-
private static _93 test;
15-
private static List<String> expected;
16-
private static List<String> actual;
17-
private static String s;
14+
private static _93.Solution1 solution1;
15+
private static List<String> expected;
16+
private static String s;
1817

19-
@BeforeClass
20-
public static void setup() {
21-
test = new _93();
22-
}
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _93.Solution1();
21+
}
2322

24-
@Before
25-
public void setupForEachTest() {
26-
expected = new ArrayList<>();
27-
actual = new ArrayList<>();
28-
}
23+
@Before
24+
public void setupForEachTest() {
25+
expected = new ArrayList<>();
26+
}
2927

30-
@Test
31-
public void test1() {
32-
s = "25525511135";
33-
expected.add("255.255.11.135");
34-
expected.add("255.255.111.35");
35-
actual = test.restoreIpAddresses(s);
36-
assertEquals(expected, actual);
37-
}
28+
@Test
29+
public void test1() {
30+
s = "25525511135";
31+
expected.add("255.255.11.135");
32+
expected.add("255.255.111.35");
33+
assertEquals(expected, solution1.restoreIpAddresses(s));
34+
}
3835
}

0 commit comments

Comments
 (0)