本文发表在 rolia.net 枫下论坛List<List<int>> Subset(List<int> OrignalSet)
{
IEnumerable<List<int>> basicSet = new List<List<int>>(OrignalSet.Distinct().Select(i => new List<int>() { i })); // Fiter out dups and create a basic set
List<List<int>> result = basicSet.ToList();
List<List<int>> tempSet;
List<string> keys = result.Select(i => i.ToString()).ToList();
List<int> temp;
basicSet = basicSet.ToList();
for (int j = 1; j < basicSet.Count(); j++ )
{
tempSet = new List<List<int>>();
foreach (List<int> item in result.Where(i => i.Count().Equals(j)) )
{
foreach (List<int> basicItem in basicSet)
{
if (!item.Contains(basicItem.First()))
{
temp = item.Union(basicItem).OrderBy(i =>i).ToList();
if (!result.Contains(temp) && !tempSet.Select(l => l.Select(i => i.ToString()).Aggregate((f, s) => f+s)).Contains(temp.Select(i => i.ToString()).Aggregate((f,s) => f+s)))
{
tempSet.Add(temp);
}
}
}
}
if (tempSet.Count > 0)
{
result.AddRange(tempSet.Distinct());
}
}
result.Add(new List<int>()); // Add an empty item
return result;
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
{
IEnumerable<List<int>> basicSet = new List<List<int>>(OrignalSet.Distinct().Select(i => new List<int>() { i })); // Fiter out dups and create a basic set
List<List<int>> result = basicSet.ToList();
List<List<int>> tempSet;
List<string> keys = result.Select(i => i.ToString()).ToList();
List<int> temp;
basicSet = basicSet.ToList();
for (int j = 1; j < basicSet.Count(); j++ )
{
tempSet = new List<List<int>>();
foreach (List<int> item in result.Where(i => i.Count().Equals(j)) )
{
foreach (List<int> basicItem in basicSet)
{
if (!item.Contains(basicItem.First()))
{
temp = item.Union(basicItem).OrderBy(i =>i).ToList();
if (!result.Contains(temp) && !tempSet.Select(l => l.Select(i => i.ToString()).Aggregate((f, s) => f+s)).Contains(temp.Select(i => i.ToString()).Aggregate((f,s) => f+s)))
{
tempSet.Add(temp);
}
}
}
}
if (tempSet.Count > 0)
{
result.AddRange(tempSet.Distinct());
}
}
result.Add(new List<int>()); // Add an empty item
return result;
}更多精彩文章及讨论,请光临枫下论坛 rolia.net