A 13-Line C# Iterative DP Solution


  • 0
    L
    public IList<IList<int>> Subsets(int[] nums) {
        List<IList<int>> result = new List<IList<int>>();
        result.Add(new List<int>());
        Array.Sort(nums);
        for (int i = 0; i < nums.Length; i++){
            List<IList<int>> newResult = new List<IList<int>>();
            foreach (var r in result){
                List<int> nr = new List<int>(r);
                nr.Add(nums[i]);
                newResult.Add(nr);
            }
            result.AddRange(newResult);
        }
        return result;
    }

Log in to reply
 

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