[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/codingOnGithub/interviews/master/leetcode/array/Subsets.java [Back]  [Original]

// Given a set of distinct integers, nums, return all possible subsets.

// Note: The solution set must not contain duplicate subsets.

// For example,
// If nums = [1,2,3], a solution is:

// [
//   [3],
//   [1],
//   [2],
//   [1,2,3],
//   [1,3],
//   [2,3],
//   [1,2],
//   []
// ]

public class Subsets {
    public List subsets(int[] nums) {
        List result = new ArrayList();
        
        recurse(result, nums, new Stack(), 0);
        
        return result;
    }
    
    private void recurse(List result, int[] nums, Stack path, int position) {
        if(position == nums.length) {
            result.add(new ArrayList(path));
            return;
        }

        path.push(nums[position]);

        recurse(result, nums, path, position + 1);
        
        path.pop();
        
        recurse(result, nums, path, position + 1);
    }
}

Web Proxy Viewer  |  New URL  |  Original Page