-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path047-Permutations2.cs
50 lines (45 loc) · 1.47 KB
/
047-Permutations2.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
49
50
//-----------------------------------------------------------------------------
// Runtime: 492ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _047_Permutations2
{
public IList<IList<int>> PermuteUnique(int[] nums)
{
var result = new List<IList<int>>();
result.Add(nums);
int length = nums.Length;
int size, temp, i, j, k;
int[] tempArray;
IList<int> tempList;
for (i = 0; i < length; i++)
{
size = result.Count;
for (j = 0; j < size; j++)
{
tempArray = result[j].ToArray();
Array.Sort(tempArray, i, length - i);
result[j] = tempArray;
for (k = i + 1; k < length; k++)
{
tempList = new List<int>(result[j]);
if (tempList[k] == tempList[k - 1] ||
tempList[k] == tempList[i])
{ continue; }
temp = tempList[k];
tempList[k] = tempList[i];
tempList[i] = temp;
result.Add(tempList);
}
}
}
return result;
}
}
}