Skip to content

Commit 3e93d1a

Browse files
committed
fd
1 parent 4ac74b9 commit 3e93d1a

File tree

2 files changed

+48
-72
lines changed

2 files changed

+48
-72
lines changed

leetcode/solution/src/RestoreIPAddresses.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.ArrayList;
2+
import java.util.Deque;
13
import java.util.LinkedList;
24
import java.util.List;
35

@@ -7,31 +9,33 @@ public class RestoreIPAddresses {
79
* 注意,0可以,但是00,01, 010这种是不允许的
810
*/
911
public List<String> restoreIpAddresses(String s) {
10-
List<String> list = new LinkedList<>();
11-
dfs(s, list, 0, 0, "");
12-
return list;
12+
List<String> result = new ArrayList<>();
13+
dfs(s, 0, new LinkedList<>(), result);
14+
return new ArrayList<>(result);
1315
}
1416

15-
private void dfs(String s, List<String> list, int index, int count, String cur) {
16-
if (index >= s.length()) {
17-
if (count == 4) {
18-
list.add(cur);
19-
}
17+
private void dfs(String s, int index, Deque<String> ips, List<String> result) {
18+
if (ips.size() > 4) {
2019
return;
2120
}
22-
23-
if (count == 4) {
21+
if (index >= s.length()) {
22+
if (ips.size() == 4) {
23+
result.add(String.join(".", ips));
24+
}
2425
return;
2526
}
26-
27-
int[][] RANGES = {
28-
{0, 0}, {0, 9}, {10, 99}, {100, 255}
29-
};
3027
for (int i = 1; i <= 3 && index + i <= s.length(); i++) {
3128
String t = s.substring(index, index + i);
32-
int n = Integer.parseInt(t);
33-
if (n >= RANGES[i][0] && n <= RANGES[i][1]) {
34-
dfs(s, list, index + i, count + 1, (cur.isEmpty() ? "" : cur + ".") + t);
29+
int k = Integer.parseInt(t);
30+
if (i == 3 && k > 255) {
31+
break;
32+
}
33+
ips.offer(t);
34+
dfs(s, index + i, ips, result);
35+
ips.pollLast();
36+
37+
if (k == 0) {
38+
break;
3539
}
3640
}
3741
}

leetcode/src/Main.java

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,46 @@
33
public class Main {
44

55
public static class Solution {
6-
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
7-
HashMap<String, HashMap<String, Double>> table = new HashMap<>();
8-
for (int i = 0; i < equations.length; i++) {
9-
add(table, equations[i], values[i]);
10-
}
11-
double[] result = new double[queries.length];
12-
HashSet<String> visited = new HashSet<>();
13-
for (int i = 0; i < queries.length; i++) {
14-
visited.clear();
15-
result[i] = calc(table, visited, queries[i][0], queries[i][1]);
16-
}
17-
return result;
6+
7+
public List<String> restoreIpAddresses(String s) {
8+
List<String> result = new ArrayList<>();
9+
dfs(s, 0, new LinkedList<>(), result);
10+
return new ArrayList<>(result);
1811
}
1912

20-
private double calc(HashMap<String, HashMap<String, Double>> table, HashSet<String> visited, String a, String b) {
21-
HashMap<String, Double> map = table.get(a);
22-
if (map == null) {
23-
return -1.0;
13+
private void dfs(String s, int index, Deque<String> ips, List<String> result) {
14+
if (ips.size() > 4) {
15+
return;
2416
}
25-
if (a.equals(b)) {
26-
return 1.0;
17+
if (index >= s.length()) {
18+
if (ips.size() == 4) {
19+
result.add(String.join(".", ips));
20+
}
21+
return;
2722
}
28-
for (Map.Entry<String, Double> entry : map.entrySet()) {
29-
String key = entry.getKey();
30-
Double value = entry.getValue();
31-
32-
if (visited.contains(key)) {
33-
continue;
23+
for (int i = 1; i <= 3 && index + i <= s.length(); i++) {
24+
String t = s.substring(index, index + i);
25+
int k = Integer.parseInt(t);
26+
if (i == 3 && k > 255) {
27+
break;
3428
}
29+
ips.offer(t);
30+
dfs(s, index + i, ips, result);
31+
ips.pollLast();
3532

36-
visited.add(key);
37-
38-
double t = calc(table, visited, key, b);
39-
if (t != -1.0) {
40-
return value * t;
33+
if (k == 0) {
34+
break;
4135
}
42-
43-
visited.remove(key);
44-
}
45-
return -1.0;
46-
}
47-
48-
private void add(HashMap<String, HashMap<String, Double>> table, String[] equation, double value) {
49-
HashMap<String, Double> map0 = table.get(equation[0]);
50-
HashMap<String, Double> map1 = table.get(equation[1]);
51-
if (map0 == null) {
52-
map0 = new HashMap<>();
53-
table.put(equation[0], map0);
54-
}
55-
if (map1 == null) {
56-
map1 = new HashMap<>();
57-
table.put(equation[1], map1);
5836
}
59-
map0.put(equation[1], value);
60-
map1.put(equation[0], 1 / value);
6137
}
6238
}
6339

6440

6541
public static void main(String[] args) {
66-
Solution s = new Solution();
67-
double[] res = s.calcEquation(new String[][]{
68-
{"a", "b"}, {"b", "c"}
69-
}, new double[]{2.0, 3.0}, new String[][]{
70-
{"a", "c"}, {"b", "a"}, {"a", "e"}, {"a", "a"}, {"x", "x"}
71-
});
72-
for (double v : res) {
73-
System.out.println(v);
42+
Solution solution = new Solution();
43+
List<String> result = solution.restoreIpAddresses("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
44+
for (String s : result) {
45+
System.out.println(s);
7446
}
7547
}
7648
}

0 commit comments

Comments
 (0)