算法--全排列、全子集、DFS\BFS问题

排列组合
子集合 subset(medium)
输入一个含有 不同数字的序列,输出所有可能的集合(含空集)。要求1 集合里元素有序排列;2 输出结果不含有重复集合;
举例:输入{1,2,3], 输出{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}
原题:https://leetcode.com/problems/subsets/
思路图:

Amortized  分期偿还(只是个名字而已)的思路: 
public static IList<IList<int>> Subsets(int[] nums)
        {
            IList<IList<int>> resSet = new List<IList<int>>();
            Array.Sort(nums);
            //初始化,加入空集
            resSet.Add(new List<int>());
            for (int i = 0; i < nums.Length; i++)
            {
                IList<IList<int>> tmp = new List<IList<int>>();
                foreach (var list in resSet)
                {
                    //保留原结果
                    tmp.Add(list);
                    List<int> clone = new List<int>(list);
                    //在原有结果里加入新元素
                    clone.Add(nums[i]);
                    tmp.Add(clone);
                }
                resSet = tmp;
            }
            return resSet;
        }
递归代码(DFS):

public static IList<IList<int>> SubSetRecrisive(int[] ints)
        {
            IList<IList<int>> resSet = new List<IList<int>>();
            if (ints == null || ints.Length == 0)
            {
                return resSet;
            }
            //Array.Sort(ints);//排序是为了方便有重复输入时判断重复;
            Helper(ints, 0, new List<int>(), resSet);
            return resSet;
        }
        public static void Helper(int[] s, int start, List<int> subset, IList<IList<int>> resSet)
        {
            //保留中间结果,不能直接添加resSet,因为这是传址,它每次都变化;
            IList<int> clone = new List<int>(subset);
            resSet.Add(clone);
            for (int i = start; i < s.Length; i++)
            {
                subset.Add(s[i]);//加入新元素,并递归调用下一个元素;
                Helper(s,i+1,subset,resSet);
                subset.RemoveAt(subset.Count-1);//退回,移除subset中最后一个索引位元素
            }
        }

备注:变种思路,如果输入数组有重复值怎么办?
答案
1 主函数中需要先排序,为Helper函数去重打基础;
2 如果s[i]==s[i-1],直接跳到下一步;


---------------------------------------------------------------------------------

全排列permutation( Medium)
题目内容,输入一个不含相同数字的序列,输出所有可能的排列permutation, 需要注意顺序
举例
输入{ 1,2,3 }
返回: {1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}
原题:https://leetcode.com/problems/permutations/

思路:与全子集相似,使用递归方法
区别: 必须等到所有数字均在集合里才能输出。所以需要辅助数组bool[] used=new bool[arr.Length]来记录元素使用情况
问题: 为什么元素不能重复使用?为什么架构与subset不同?
自我解答,欢迎补充:使用1,2作为输入,确实能够得到答案,只能说此架构设计比较巧妙。长期来看只能靠记忆
public static IList<IList<int>> PermuteRecrusive(int[] ints)
      {
            IList<IList<int>> resSet = new List<IList<int>>();
            if (ints == null || ints.Length == 0)
            {
                return resSet;
            }
            Array.Sort(ints);
 
            PermuteHelper(ints, new List<int>(), resSet);
             return resSet;
        }
        /// <summary>
        /// 1 为什么循环中不需要pos参数? 2为什么参数不能重复使用?
        /// </summary>
        /// <param name="ints"></param>
        /// <param name="subset"></param>
        /// <param name="resSet"></param>
        /// <param name="used"></param>
        public static void PermuteHelper(int[] ints, List<int> subset, IList<IList<int>> resSet)
        {
            if (subset.Count == ints.Length)
            {
                resSet.Add(new List<int>(subset));
                return; //为什么需要这个?因为add后需要回到上一层递归;
            }
            for (int i = 0; i < ints.Length; i++)
            {
                if (subset.Contains(ints[i])) continue; //不能重复使用已有元素,为什么?
                subset.Add(ints[i]);//加入新元素,递归调用下一个元素
                PermuteHelper(ints, subset, resSet);
                subset.RemoveAt(subset.Count-1);//回退
            }
        }



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值