-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path093-RestoreIPAddresses.cs
48 lines (42 loc) · 1.52 KB
/
093-RestoreIPAddresses.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//-----------------------------------------------------------------------------
// Runtime: 292ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _093_RestoreIPAddresses
{
public IList<string> RestoreIpAddresses(string s)
{
var results = new List<string>();
RestoreIpAddresses(s, 0, "", 0, results);
return results;
}
private void RestoreIpAddresses(string s, int startIndex, string existedString, int depth, List<string> results)
{
if (startIndex == s.Length && depth == 4)
{
results.Add(existedString);
return;
}
if (startIndex == s.Length || depth == 4) { return; }
for (int i = 1; i < 4; i++)
{
if ((startIndex + i > s.Length) || (i == 2 && s[startIndex] == '0'))
break;
if (i == 3)
{
var num = int.Parse(s.Substring(startIndex, i));
if (num > 255)
break;
}
var newString = string.IsNullOrWhiteSpace(existedString)
? s.Substring(startIndex, i)
: string.Join(".", existedString, s.Substring(startIndex, i));
RestoreIpAddresses(s, startIndex + i, newString, depth + 1, results);
}
}
}
}