LeetCode-46&47.Permutations

Given a collection of distinct numbers, 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], and [3,2,1].

public class Solution
{
    public IList<IList<int>> Permute(int[] nums) 
    {
        IList<IList<int>> res = new List<IList<int>>();
        IList<int> list = new List<int>();
        Fun(res, list, new List<int>(nums));
        return res;
    }
    
    private void Fun(IList<IList<int>> res, IList<int> list, IList<int> nums)
    {
        if (1 == nums.Count)
        {
            list.Add(nums[0]);
            res.Add(new List<int>(list));
            list.Remove(nums[0]);
            return;
        }

        for (int j = 0; j < nums.Count; j++)
        {
            list.Add(nums[j]);
            IList<int> cur = new List<int>(nums);
            cur.RemoveAt(j);
            Fun(res, list, cur);
            list.Remove(nums[j]);
        }
    }
}

思路一样实现不一样的解法:(效率低)
public class Solution
{
    public IList<IList<int>> Permute(int[] nums) 
    {
        IList<IList<int>> res = new List<IList<int>>();
        IList <int> list = new List<int>();
        Fun(res,list,nums);
        return res;
    }
    
    private void Fun(IList<IList<int>> res, IList<int> list,int[] nums)
    {
        if (list.Count == nums.Length)
        {
            res.Add(new List<int>(list));
            return;
        }   
        
        for (int j = 0; j < nums.Length; j++)
        {
            if (!list.Contains(nums[j]))
            {
                list.Add(nums[j]);
                Fun(res, list,nums);
                list.Remove(nums[j]);
            }
        }
    }
}

用交换nums数值取代list
public class Solution
{
    public IList<IList<int>> Permute(int[] nums) 
    {
        IList<IList<int>> res = new List<IList<int>>();
        IList <int> list = new List<int>();
        Fun(res,0,nums);
        return res;
    }
    
    private void Fun(IList<IList<int>> res, int i,int[] nums)
    {
        if (i == nums.Length)
        {
            res.Add(new List<int>(nums));
            return;
        }   
        
        for (int j = i; j < nums.Length; j++)
        {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
            Fun(res, i + 1, nums);
            tmp = nums[j];
            nums[j] = nums[i];
            nums[i] = tmp;
        }
    }
}

非递归解:(参考: https://leetcode.com/discuss/19510/my-ac-simple-iterative-java-python-solution)
public IList<IList<int>> Permute(int[] nums)
        {
            IList<IList<int>> res = new List<IList<int>>();
            if (nums.Length == 0) return res;
            List<int> list = new List<int>();
            list.Add(nums[0]);
            res.Add(list);
            for (int i = 1; i < nums.Length;i++)
            {
                IList<IList<int>> new_ans = new List<IList<int>>();
                for (int j = 0; j <= i; ++j)
                {
                    foreach (List<int> item in res)
                    {
                        List<int> new_l = new List<int>(item);
                        new_l.Insert(j, nums[i]);
                        new_ans.Add(new_l);
                    }
                }
                res = new_ans;
            }
            return res;
        }

47、nums中有重复数字,进行全排列
参考: https://leetcode.com/discuss/73856/really-easy-solution-easier-than-solutions-with-very-high-vote
public class Solution {
    public IList<IList<int>> PermuteUnique(int[] nums) 
    {
        IList<IList<int>> res = new List<IList<int>>();
        if (nums == null || nums.Length == 0) return res;
        IList<int> list = new List<int>();
        Array.Sort(nums);
        bool[] used = new bool[nums.Length];
        Fun(res, list, nums, used);
        return res;
    }
    
    private void Fun(IList<IList<int>> res, IList<int> list, int[] nums,bool[] used)
    {
        if (list.Count == nums.Length)
        {
            res.Add(new List<int>(list));
            return;
        }

        for (int i = 0; i < nums.Length; i++)
        {
            if (used[i]) continue;
            if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) continue;
            used[i] = true;

            list.Add(nums[i]);
            Fun(res, list, nums, used);
            used[i] = false;
            list.RemoveAt(list.Count - 1);
        }
    }
}


你可能感兴趣的:(LeetCode)