|
3 | 3 | public class Main {
|
4 | 4 |
|
5 | 5 | 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); |
18 | 11 | }
|
19 | 12 |
|
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; |
24 | 16 | }
|
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; |
27 | 22 | }
|
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; |
34 | 28 | }
|
| 29 | + ips.offer(t); |
| 30 | + dfs(s, index + i, ips, result); |
| 31 | + ips.pollLast(); |
35 | 32 |
|
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; |
41 | 35 | }
|
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); |
58 | 36 | }
|
59 |
| - map0.put(equation[1], value); |
60 |
| - map1.put(equation[0], 1 / value); |
61 | 37 | }
|
62 | 38 | }
|
63 | 39 |
|
64 | 40 |
|
65 | 41 | 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); |
74 | 46 | }
|
75 | 47 | }
|
76 | 48 | }
|
0 commit comments