递归获取所有子集

mnxm 2014-04-26 11:52:39
static void Main(string[] args)
{
List<int> lstTemp = new List<int>() { 1, 2, 3 };
GetChildCollections(lstTemp);
}

static List<List<int>> GetChildCollections(List<int> lstTemp)
{

}


//结果
123
12
3
2
1
13
23

求优雅的算法实现
...全文
607 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
iyomumx 2014-04-27
  • 打赏
  • 举报
回复
放狗一搜一大堆何必呢:
	static List<List<int>> GetChildCollections(List<int> lstTemp)
	{
		var subsets = from m in Enumerable.Range(0, 1 << lstTemp.Count)
					  select
						  (from i in Enumerable.Range(0, lstTemp.Count)
						  where (m & (1 << i)) != 0
						  select lstTemp[i]).ToList();
		return subsets.ToList();
	}
	
注:含空集
threenewbee 2014-04-27
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390550326 用我的代码,再加一个循环就搞定了

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧