I took a liberty of copying this code and adjusted it to C#
private static List<List<int>> Subsets(List<int> A)
{
List<int> subset = new List<int>();
List<List<int>> res = new List<List<int>>();
int index = 0;
SubsetsUtil(A, ref res, ref subset, index);
//here res becomes "empty"
return res;
}
private static void SubsetsUtil(List<int> A, ref List<List<int>> res, ref List<int> subset, int index)
{
if (subset.Count > 0)
{
res.Add(subset);
}
for (int i = index; i < A.Count(); i++)
{
// include the A[i] in subset.
subset.Add(A[i]);
// move onto the next element.
SubsetsUtil(A, ref res, ref subset, i + 1);
// exclude the A[i] from subset and triggers
// backtracking.
subset.RemoveAt(subset.Count - 1);
}
return;
}
On paper it looks amazing, but for some reason Subsets
method returns list
that contains proper count of subsets, but they are all empty. When I placed some traps inside SubsetsUtil
method results are actually saving properly to res
variable, but after SubsetsUtil
method is finished inside Subsets
, res
no longer contains it.
I have absolutely no idea how to fix it, it looks like a glitch in environment, but I'm pretty sure I'm just missing some little tiny detail.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…