mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -773,7 +773,39 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public List<IList<int>> res = new List<IList<int>>();
|
||||
public List<int> path = new List<int>();
|
||||
public IList<IList<int>> CombinationSum2(int[] candidates, int target)
|
||||
{
|
||||
|
||||
Array.Sort(candidates);
|
||||
BackTracking(candidates, target, 0, 0);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int[] candidates, int target, int start, int sum)
|
||||
{
|
||||
if (sum > target) return;
|
||||
if (sum == target)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++)
|
||||
{
|
||||
if (i > start && candidates[i] == candidates[i - 1]) continue;
|
||||
sum += candidates[i];
|
||||
path.Add(candidates[i]);
|
||||
BackTracking(candidates, target, i + 1, sum);
|
||||
sum -= candidates[i];
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -799,6 +799,53 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<string> res = new List<string>();
|
||||
public IList<string> RestoreIpAddresses(string s)
|
||||
{
|
||||
if (s.Length < 4 || s.Length > 12) return res;
|
||||
BackTracking(s, 0, 0);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(string s, int start, int pointSum)
|
||||
{
|
||||
if (pointSum == 3)
|
||||
{
|
||||
if (IsValid(s, start, s.Length - 1))
|
||||
{
|
||||
res.Add(s);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (int i = start; i < s.Length; i++)
|
||||
{
|
||||
if (IsValid(s, start, i))
|
||||
{
|
||||
s = s.Insert(i + 1, ".");
|
||||
BackTracking(s, i + 2, pointSum + 1);
|
||||
s = s.Remove(i + 1, 1);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
}
|
||||
public bool IsValid(string s, int start, int end)
|
||||
{
|
||||
if (start > end) return false;
|
||||
if (s[start] == '0' && start != end) return false;
|
||||
int num = 0;
|
||||
for (int i = start; i <= end; i++)
|
||||
{
|
||||
if (s[i] > '9' || s[i] < '0') return false;
|
||||
num = num * 10 + s[i] - '0';
|
||||
if (num > 255) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
@ -847,6 +847,50 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### CSharp
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<string>> res = new List<IList<string>>();
|
||||
public IList<string> path = new List<string>();
|
||||
public IList<IList<string>> Partition(string s)
|
||||
{
|
||||
BackTracking(s, 0);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(string s, int start)
|
||||
{
|
||||
if (start >= s.Length)
|
||||
{
|
||||
res.Add(new List<string>(path));
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = start; i < s.Length; i++)
|
||||
{
|
||||
if (IsPalindrome(s, start, i))
|
||||
{
|
||||
path.Add(s.Substring(start, i - start + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
BackTracking(s, i + 1);
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
public bool IsPalindrome(string s, int start, int end)
|
||||
{
|
||||
for (int i = start, j = end; i < j; i++, j--)
|
||||
{
|
||||
if (s[i] != s[j])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
Reference in New Issue
Block a user