|
6 | 6 | /** |
7 | 7 | * 93. Restore IP Addresses |
8 | 8 | * |
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) |
15 | 15 | */ |
16 | 16 | public class _93 { |
17 | 17 |
|
| 18 | + public static class Solution1 { |
18 | 19 | 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) { |
24 | 22 | return allValidIpAddresses; |
| 23 | + } |
| 24 | + backtracking(s, new ArrayList<>(), allValidIpAddresses, 0); |
| 25 | + return allValidIpAddresses; |
25 | 26 | } |
26 | 27 |
|
27 | 28 | 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; |
40 | 32 | } |
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; |
50 | 47 | } |
| 48 | + bytes.add(oneByte); |
| 49 | + backtracking(s, bytes, result, i + 1); |
| 50 | + bytes.remove(bytes.size() - 1); |
| 51 | + } |
51 | 52 | } |
52 | 53 |
|
53 | 54 | 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); |
59 | 60 | } |
60 | | - |
| 61 | + } |
61 | 62 | } |
0 commit comments