forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
33 lines (31 loc) · 828 Bytes
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Solution {
private IList<string> ans = new List<string>();
private IList<string> t = new List<string>();
private int n;
private string s;
public IList<string> RestoreIpAddresses(string s) {
n = s.Length;
this.s = s;
dfs(0);
return ans;
}
private void dfs(int i) {
if (i >= n && t.Count == 4) {
ans.Add(string.Join(".", t));
return;
}
if (i >= n || t.Count == 4) {
return;
}
int x = 0;
for (int j = i; j < i + 3 && j < n; ++j) {
x = x * 10 + (s[j] - '0');
if (x > 255 || (j > i && s[i] == '0')) {
break;
}
t.Add(x.ToString());
dfs(j + 1);
t.RemoveAt(t.Count - 1);
}
}
}