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 32843da

Browse files
committed
Update0093.复原IP地址,添加C#
1 parent 69a38ff commit 32843da

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎problems/0093.复原IP地址.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,53 @@ object Solution {
799799
}
800800
}
801801
```
802+
### C#
803+
```csharp
804+
public class Solution
805+
{
806+
public IList<string> res = new List<string>();
807+
public IList<string> RestoreIpAddresses(string s)
808+
{
809+
if (s.Length < 4 || s.Length > 12) return res;
810+
BackTracking(s, 0, 0);
811+
return res;
812+
}
813+
public void BackTracking(string s, int start, int pointSum)
814+
{
815+
if (pointSum == 3)
816+
{
817+
if (IsValid(s, start, s.Length - 1))
818+
{
819+
res.Add(s);
820+
}
821+
return;
822+
}
823+
for (int i = start; i < s.Length; i++)
824+
{
825+
if (IsValid(s, start, i))
826+
{
827+
s = s.Insert(i + 1, ".");
828+
BackTracking(s, i + 2, pointSum + 1);
829+
s = s.Remove(i + 1, 1);
830+
}
831+
else break;
832+
}
833+
}
834+
public bool IsValid(string s, int start, int end)
835+
{
836+
if (start > end) return false;
837+
if (s[start] == '0' && start != end) return false;
838+
int num = 0;
839+
for (int i = start; i <= end; i++)
840+
{
841+
if (s[i] > '9' || s[i] < '0') return false;
842+
num = num * 10 + s[i] - '0';
843+
if (num > 255) return false;
844+
}
845+
return true;
846+
}
847+
}
848+
```
802849

803850

804851
<p align="center">

0 commit comments

Comments
(0)

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