mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
@ -486,6 +486,37 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> Permute(int[] nums)
|
||||
{
|
||||
var used = new bool[nums.Length];
|
||||
BackTracking(nums, used);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int[] nums, bool[] used)
|
||||
{
|
||||
if (path.Count == nums.Length)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
if (used[i]) continue;
|
||||
used[i] = true;
|
||||
path.Add(nums[i]);
|
||||
BackTracking(nums, used);
|
||||
used[i] = false;
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -614,6 +614,32 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution {
|
||||
public IList<IList<int>> res = new List<IList<int>>();
|
||||
public IList<int> path = new List<int>();
|
||||
public IList<IList<int>> FindSubsequences(int[] nums) {
|
||||
BackTracking(nums, 0);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int[] nums, int start){
|
||||
if(path.Count >= 2){
|
||||
res.Add(new List<int>(path));
|
||||
}
|
||||
HashSet<int> hs = new HashSet<int>();
|
||||
for(int i = start; i < nums.Length; i++){
|
||||
if(path.Count > 0 && path[path.Count - 1] > nums[i] || hs.Contains(nums[i])){
|
||||
continue;
|
||||
}
|
||||
hs.Add(nums[i]);
|
||||
path.Add(nums[i]);
|
||||
BackTracking(nums, i + 1);
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user