C# non recursive


  • 0
    H
    public IList<IList<int>> Subsets(int[] nums) {
            List<IList<int>> sets = new List<IList<int>>();
            
            sets.Add(new List<int>());
            foreach(int n in nums) {
                List<IList<int>> newSets = new List<IList<int>>();
                foreach(IList<int> list in sets) {
                    List<int> newList = new List<int>(list);
                    newList.Add(n);
                    
                    newSets.Add(newList);
                }
                
                sets.AddRange(newSets);
            }
            
            return sets;
        }
    

Log in to reply
 

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