Permutations II, Please help


  • 0

    Failed on test case [-1,2,0,-1,1,0,1], can't figure it out the reason, please help, thanks

    public class Solution {
        public IList<IList<int>> PermuteUnique(int[] nums) 
        {
            IList<IList<int>> rslist=new List<IList<int>>();
            Array.Sort(nums);
            return helper(rslist,nums,0);
        }
        
        public IList<IList<int>> helper(IList<IList<int>> rslist,int[] nums,int begin)
        {
            if(begin==nums.Length-1)
            {
                rslist.Add(nums.ToList());
            }
            else
            {
                for(int i=begin;i<nums.Length;i++)
                {
                    if(i>begin && (nums[i]==nums[begin] || nums[i]==nums[i-1]))
                    {
                        continue;
                    }
                    nums=swap(nums,begin,i);
                    rslist=helper(rslist,nums,begin+1);
                    nums=swap(nums,i,begin);
                }
            }
            return rslist;
        }
        
        public int[] swap(int[] nums,int begin,int end)
        {
            int temp=nums[begin];
            nums[begin]=nums[end];
            nums[end]=temp;
            return nums;
        }
        
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.