BFS solution in C#


  • 1
    C
    public IList<IList<int>> Permute(int[] nums) {
            IList<IList<int>> res = new List<IList<int>>();
            Queue<IList<int>> q = new Queue<IList<int>>();
            q.Enqueue(new List<int>());
            
            for(int i=0;i<nums.Length;i++){
                int size = q.Count;
                
                while(size-- >0){
                    IList<int> list = q.Dequeue();
                    for(int j=0;j<=list.Count;j++){
                        List<int> templist = new List<int>(list);
                        templist.Insert(j,nums[i]);
                        q.Enqueue(templist);
                    }
                }
            }
            
            while(q.Count>0){
                res.Add(q.Dequeue());
            }
            
            return res;
        }
    

Log in to reply
 

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