-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPermutations.cs
executable file
·89 lines (84 loc) · 4.94 KB
/
Permutations.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Source : https://leetcode.com/problems/permutations/
// Author : codeyu
// Date : Tuesday, October 25, 2016 11:19:51 PM
/**********************************************************************************
*
*
* Given a collection of distinct numsbers, return all possible permutations.
*
*
*
* For example,
* [1,2,3] have the following permutations:
*
* [
* [1,2,3],
* [1,3,2],
* [2,1,3],
* [2,3,1],
* [3,1,2],
* [3,2,1]
* ]
*
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution046
{
public static IList<IList<int>> Permute(int[] nums)
{
IList<IList<int>> result = new List<IList<int>>();
if(nums==null || nums.Length==0) return result;
helper(nums, new bool[nums.Length], new List<int>(), result);
return result;
}
private static void helper(int[] nums, bool[] used, IList<int> item, IList<IList<int>> result)
{
if(item.Count == nums.Length)
{
result.Add(new List<int>(item));
return;
}
for(int i=0;i<nums.Length;i++)
{
if(!used[i])
{
used[i] = true;
item.Add(nums[i]);
helper(nums, used, item, result);
item.RemoveAt(item.Count - 1);
used[i] = false;
}
}
}
public IList<IList<int>> Permute2(int[] nums)
{
IList<IList<int>> result = new List<IList<int>>();
if(nums == null || nums.Length==0) return result;
IList<int> first = new List<int>();
first.Add(nums[0]);
result.Add(first);
for(int i=1;i<nums.Length;i++)
{
IList<IList<int>> newresult = new List<IList<int>>();
for(int j=0;j<result.Count;j++)
{
IList<int> cur = result[j];
for(int k=0;k<cur.Count+1;k++)
{
IList<int> item = new List<int>(cur);
item.Insert(k,nums[i]);
newresult.Add(item);
}
}
result = newresult;
}
return result;
}
}
}