Skip to content

Commit cd192e6

Browse files
refactor 93
1 parent 19f54ff commit cd192e6

File tree

1 file changed

+39
-49
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+39
-49
lines changed

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

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,50 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 93. Restore IP Addresses
8-
*
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)
15-
*/
166
public class _93 {
177

18-
public static class Solution1 {
19-
public List<String> restoreIpAddresses(String s) {
20-
List<String> allValidIpAddresses = new ArrayList<>();
21-
if (s == null || s.length() > 12 || s.length() < 4) {
22-
return allValidIpAddresses;
23-
}
24-
backtracking(s, new ArrayList<>(), allValidIpAddresses, 0);
25-
return allValidIpAddresses;
26-
}
27-
28-
private void backtracking(String s, ArrayList<String> bytes, List<String> result, int pos) {
29-
if (bytes.size() == 4) {
30-
if (pos != s.length()) {
31-
return;
32-
}
33-
StringBuilder stringBuilder = new StringBuilder();
34-
for (int i = 0; i < 4; i++) {
35-
stringBuilder.append(bytes.get(i));
36-
stringBuilder.append(".");
8+
public static class Solution1 {
9+
public List<String> restoreIpAddresses(String s) {
10+
List<String> allValidIpAddresses = new ArrayList<>();
11+
if (s == null || s.length() > 12 || s.length() < 4) {
12+
return allValidIpAddresses;
13+
}
14+
backtracking(s, new ArrayList<>(), allValidIpAddresses, 0);
15+
return allValidIpAddresses;
3716
}
38-
stringBuilder.setLength(stringBuilder.length() - 1);
39-
result.add(stringBuilder.toString());
40-
return;
41-
}
4217

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;
18+
private void backtracking(String s, ArrayList<String> bytes, List<String> result, int pos) {
19+
if (bytes.size() == 4) {
20+
if (pos != s.length()) {
21+
return;
22+
}
23+
StringBuilder stringBuilder = new StringBuilder();
24+
for (int i = 0; i < 4; i++) {
25+
stringBuilder.append(bytes.get(i));
26+
stringBuilder.append(".");
27+
}
28+
stringBuilder.setLength(stringBuilder.length() - 1);
29+
result.add(stringBuilder.toString());
30+
return;
31+
}
32+
33+
for (int i = pos; i < pos + 4 && i < s.length(); i++) {
34+
String oneByte = s.substring(pos, i + 1);
35+
if (!isValid(oneByte)) {
36+
continue;
37+
}
38+
bytes.add(oneByte);
39+
backtracking(s, bytes, result, i + 1);
40+
bytes.remove(bytes.size() - 1);
41+
}
4742
}
48-
bytes.add(oneByte);
49-
backtracking(s, bytes, result, i + 1);
50-
bytes.remove(bytes.size() - 1);
51-
}
52-
}
5343

54-
private boolean isValid(String oneByte) {
55-
if (oneByte.charAt(0) == '0') {
56-
return oneByte.equals("0");
57-
}
58-
int num = Integer.valueOf(oneByte);
59-
return (num >= 0 && num < 256);
44+
private boolean isValid(String oneByte) {
45+
if (oneByte.charAt(0) == '0') {
46+
return oneByte.equals("0");
47+
}
48+
int num = Integer.valueOf(oneByte);
49+
return (num >= 0 && num < 256);
50+
}
6051
}
61-
}
6252
}

0 commit comments

Comments
 (0)