Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3d25f7d

Browse files
committed
feat: add solutions to lc/lcof2 problems
lc No.0093 & lcof2 No.087. Restore IP Addresses
1 parent ffd8742 commit 3d25f7d

File tree

9 files changed

+528
-78
lines changed

9 files changed

+528
-78
lines changed

‎lcof2/剑指 Offer II 087. 复原 IP/README.md‎

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,136 @@
6060

6161
<p><meta charset="UTF-8" />注意:本题与主站 93&nbsp;题相同:<a href="https://leetcode-cn.com/problems/restore-ip-addresses/">https://leetcode-cn.com/problems/restore-ip-addresses/</a>&nbsp;</p>
6262

63-
6463
## 解法
6564

6665
<!-- 这里可写通用的实现逻辑 -->
6766

67+
DFS。
68+
6869
<!-- tabs:start -->
6970

7071
### **Python3**
7172

7273
<!-- 这里可写当前语言的特殊实现逻辑 -->
7374

7475
```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
76100
```
77101

78102
### **Java**
79103

80104
<!-- 这里可写当前语言的特殊实现逻辑 -->
81105

82106
```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+
```
83148

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+
};
84193
```
85194

86195
### **...**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public:
3+
vector<string> restoreIpAddresses(string s) {
4+
vector<string> ans;
5+
vector<string> t;
6+
dfs(s, t, ans);
7+
return ans;
8+
}
9+
10+
void dfs(string s, vector<string>& t, vector<string>& ans) {
11+
if (t.size() == 4)
12+
{
13+
if (s == "")
14+
{
15+
string p = "";
16+
for (auto e : t) p += e + ".";
17+
p.pop_back();
18+
ans.push_back(p);
19+
}
20+
return;
21+
}
22+
for (int i = 1; i < min(4, (int) s.size() + 1); ++i)
23+
{
24+
string c = s.substr(0, i);
25+
if (check(c))
26+
{
27+
t.push_back(c);
28+
dfs(s.substr(i, s.size() - i), t, ans);
29+
t.pop_back();
30+
}
31+
}
32+
}
33+
34+
bool check(string s) {
35+
if (s == "") return false;
36+
int num = stoi(s);
37+
if (num > 255) return false;
38+
if (s[0] == '0' && s.size() > 1) return false;
39+
return true;
40+
}
41+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
private List<String> ans;
3+
4+
public List<String> restoreIpAddresses(String s) {
5+
ans = new ArrayList<>();
6+
dfs(s, new ArrayList<>());
7+
return ans;
8+
}
9+
10+
private void dfs(String s, List<String> t) {
11+
if (t.size() == 4) {
12+
if ("".equals(s)) {
13+
ans.add(String.join(".", t));
14+
}
15+
return;
16+
}
17+
for (int i = 1; i < Math.min(4, s.length() + 1); ++i) {
18+
String c = s.substring(0, i);
19+
if (check(c)) {
20+
t.add(c);
21+
dfs(s.substring(i), t);
22+
t.remove(t.size() - 1);
23+
}
24+
}
25+
}
26+
27+
private boolean check(String s) {
28+
if ("".equals(s)) {
29+
return false;
30+
}
31+
int num = Integer.parseInt(s);
32+
if (num > 255) {
33+
return false;
34+
}
35+
if (s.charAt(0) == '0' && s.length() > 1) {
36+
return false;
37+
}
38+
return true;
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def restoreIpAddresses(self, s: str) -> List[str]:
3+
ans = []
4+
5+
def check(s):
6+
if not (0 <= int(s) <= 255):
7+
return False
8+
if s[0] == '0' and len(s) > 1:
9+
return False
10+
return True
11+
12+
def dfs(s, t):
13+
if len(t) == 4:
14+
if not s:
15+
ans.append('.'.join(t))
16+
return
17+
for i in range(1, min(4, len(s) + 1)):
18+
if check(s[:i]):
19+
t.append(s[:i])
20+
dfs(s[i:], t)
21+
t.pop()
22+
23+
dfs(s, [])
24+
return ans

‎solution/0000-0099/0093.Restore IP Addresses/README.md‎

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,136 @@
5858
<li><code>s</code> 仅由数字组成</li>
5959
</ul>
6060

61-
6261
## 解法
6362

6463
<!-- 这里可写通用的实现逻辑 -->
6564

65+
DFS。
66+
6667
<!-- tabs:start -->
6768

6869
### **Python3**
6970

7071
<!-- 这里可写当前语言的特殊实现逻辑 -->
7172

7273
```python
73-
74+
class Solution:
75+
def restoreIpAddresses(self, s: str) -> List[str]:
76+
ans = []
77+
78+
def check(s):
79+
if not (0 <= int(s) <= 255):
80+
return False
81+
if s[0] == '0' and len(s) > 1:
82+
return False
83+
return True
84+
85+
def dfs(s, t):
86+
if len(t) == 4:
87+
if not s:
88+
ans.append('.'.join(t))
89+
return
90+
for i in range(1, min(4, len(s) + 1)):
91+
if check(s[:i]):
92+
t.append(s[:i])
93+
dfs(s[i:], t)
94+
t.pop()
95+
96+
dfs(s, [])
97+
return ans
7498
```
7599

76100
### **Java**
77101

78102
<!-- 这里可写当前语言的特殊实现逻辑 -->
79103

80104
```java
105+
class Solution {
106+
private List<String> ans;
107+
108+
public List<String> restoreIpAddresses(String s) {
109+
ans = new ArrayList<>();
110+
dfs(s, new ArrayList<>());
111+
return ans;
112+
}
113+
114+
private void dfs(String s, List<String> t) {
115+
if (t.size() == 4) {
116+
if ("".equals(s)) {
117+
ans.add(String.join(".", t));
118+
}
119+
return;
120+
}
121+
for (int i = 1; i < Math.min(4, s.length() + 1); ++i) {
122+
String c = s.substring(0, i);
123+
if (check(c)) {
124+
t.add(c);
125+
dfs(s.substring(i), t);
126+
t.remove(t.size() - 1);
127+
}
128+
}
129+
}
130+
131+
private boolean check(String s) {
132+
if ("".equals(s)) {
133+
return false;
134+
}
135+
int num = Integer.parseInt(s);
136+
if (num > 255) {
137+
return false;
138+
}
139+
if (s.charAt(0) == '0' && s.length() > 1) {
140+
return false;
141+
}
142+
return true;
143+
}
144+
}
145+
```
81146

147+
### **C++**
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
vector<string> restoreIpAddresses(string s) {
153+
vector<string> ans;
154+
vector<string> t;
155+
dfs(s, t, ans);
156+
return ans;
157+
}
158+
159+
void dfs(string s, vector<string>& t, vector<string>& ans) {
160+
if (t.size() == 4)
161+
{
162+
if (s == "")
163+
{
164+
string p = "";
165+
for (auto e : t) p += e + ".";
166+
p.pop_back();
167+
ans.push_back(p);
168+
}
169+
return;
170+
}
171+
for (int i = 1; i < min(4, (int) s.size() + 1); ++i)
172+
{
173+
string c = s.substr(0, i);
174+
if (check(c))
175+
{
176+
t.push_back(c);
177+
dfs(s.substr(i, s.size() - i), t, ans);
178+
t.pop_back();
179+
}
180+
}
181+
}
182+
183+
bool check(string s) {
184+
if (s == "") return false;
185+
int num = stoi(s);
186+
if (num > 255) return false;
187+
if (s[0] == '0' && s.size() > 1) return false;
188+
return true;
189+
}
190+
};
82191
```
83192

84193
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /