|
60 | 60 |
|
61 | 61 | <p><meta charset="UTF-8" />注意:本题与主站 93 题相同:<a href="https://leetcode-cn.com/problems/restore-ip-addresses/">https://leetcode-cn.com/problems/restore-ip-addresses/</a> </p>
|
62 | 62 |
|
63 |
| - |
64 | 63 | ## 解法
|
65 | 64 |
|
66 | 65 | <!-- 这里可写通用的实现逻辑 -->
|
67 | 66 |
|
| 67 | +DFS。 |
| 68 | + |
68 | 69 | <!-- tabs:start -->
|
69 | 70 |
|
70 | 71 | ### **Python3**
|
71 | 72 |
|
72 | 73 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
73 | 74 |
|
74 | 75 | ```python
|
75 |
| - |
| 76 | +class Solution: |
| 77 | + def restoreIpAddresses(self, s: str) -> List[str]: |
| 78 | + ans = [] |
| 79 | + |
| 80 | + def check(s): |
| 81 | + if not (0 <= int(s) <= 255): |
| 82 | + return False |
| 83 | + if s[0] == '0' and len(s) > 1: |
| 84 | + return False |
| 85 | + return True |
| 86 | + |
| 87 | + def dfs(s, t): |
| 88 | + if len(t) == 4: |
| 89 | + if not s: |
| 90 | + ans.append('.'.join(t)) |
| 91 | + return |
| 92 | + for i in range(1, min(4, len(s) + 1)): |
| 93 | + if check(s[:i]): |
| 94 | + t.append(s[:i]) |
| 95 | + dfs(s[i:], t) |
| 96 | + t.pop() |
| 97 | + |
| 98 | + dfs(s, []) |
| 99 | + return ans |
76 | 100 | ```
|
77 | 101 |
|
78 | 102 | ### **Java**
|
79 | 103 |
|
80 | 104 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
81 | 105 |
|
82 | 106 | ```java
|
| 107 | +class Solution { |
| 108 | + private List<String> ans; |
| 109 | + |
| 110 | + public List<String> restoreIpAddresses(String s) { |
| 111 | + ans = new ArrayList<>(); |
| 112 | + dfs(s, new ArrayList<>()); |
| 113 | + return ans; |
| 114 | + } |
| 115 | + |
| 116 | + private void dfs(String s, List<String> t) { |
| 117 | + if (t.size() == 4) { |
| 118 | + if ("".equals(s)) { |
| 119 | + ans.add(String.join(".", t)); |
| 120 | + } |
| 121 | + return; |
| 122 | + } |
| 123 | + for (int i = 1; i < Math.min(4, s.length() + 1); ++i) { |
| 124 | + String c = s.substring(0, i); |
| 125 | + if (check(c)) { |
| 126 | + t.add(c); |
| 127 | + dfs(s.substring(i), t); |
| 128 | + t.remove(t.size() - 1); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private boolean check(String s) { |
| 134 | + if ("".equals(s)) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + int num = Integer.parseInt(s); |
| 138 | + if (num > 255) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + if (s.charAt(0) == '0' && s.length() > 1) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + return true; |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
83 | 148 |
|
| 149 | +### **C++** |
| 150 | + |
| 151 | +```cpp |
| 152 | +class Solution { |
| 153 | +public: |
| 154 | + vector<string> restoreIpAddresses(string s) { |
| 155 | + vector<string> ans; |
| 156 | + vector<string> t; |
| 157 | + dfs(s, t, ans); |
| 158 | + return ans; |
| 159 | + } |
| 160 | + |
| 161 | + void dfs(string s, vector<string>& t, vector<string>& ans) { |
| 162 | + if (t.size() == 4) |
| 163 | + { |
| 164 | + if (s == "") |
| 165 | + { |
| 166 | + string p = ""; |
| 167 | + for (auto e : t) p += e + "."; |
| 168 | + p.pop_back(); |
| 169 | + ans.push_back(p); |
| 170 | + } |
| 171 | + return; |
| 172 | + } |
| 173 | + for (int i = 1; i < min(4, (int) s.size() + 1); ++i) |
| 174 | + { |
| 175 | + string c = s.substr(0, i); |
| 176 | + if (check(c)) |
| 177 | + { |
| 178 | + t.push_back(c); |
| 179 | + dfs(s.substr(i, s.size() - i), t, ans); |
| 180 | + t.pop_back(); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + bool check(string s) { |
| 186 | + if (s == "") return false; |
| 187 | + int num = stoi(s); |
| 188 | + if (num > 255) return false; |
| 189 | + if (s[0] == '0' && s.size() > 1) return false; |
| 190 | + return true; |
| 191 | + } |
| 192 | +}; |
84 | 193 | ```
|
85 | 194 |
|
86 | 195 | ### **...**
|
|
0 commit comments