Subsets II

public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (nums == null || nums.length < 1) {
            return result;
        }
        List<Integer> level = new ArrayList<Integer>();
        Arrays.sort(nums);
        dfs(result, level, 0, nums);
        return result;
    }
    private void dfs(List<List<Integer>> result, List<Integer> level, int loc, int[] nums) {
        result.add(new ArrayList<Integer>(level));
        for (int i = loc; i < nums.length; i++) {
            if (i != loc&& nums[i] == nums[i - 1]) {
                continue;
            }
            level.add(nums[i]);
            dfs(result, level, i + 1, nums);
            level.remove(level.size() - 1);
        }
    }
}

阅读更多
文章标签: Array Backtracking
个人分类: Leetcode 刷题
上一篇Combination Sum III
下一篇Search for a range
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭