TypechoJoeTheme

IT技术分享

统计

[LeetCode 90] Subsets II [Java]

2018-03-25
/
0 评论
/
855 阅读
/
正在检测是否收录...
03/25

1. Description

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

2. Example

If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

3. Code

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeetCode0090 {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        Arrays.sort(nums);
        dfs(nums, 0, result, new ArrayList<Integer>());
        return result;
    }

    private void dfs(int[] nums, int index, List<List<Integer>> result, List<Integer> current) {
        result.add(current);
        for (int i = index; i < nums.length; i++) {
            if (i > index && nums[i] == nums[i - 1]) {
                continue;
            }
            current.add(nums[i]);
            dfs(nums, i + 1, result, new ArrayList<Integer>(current));
            current.remove(current.size() - 1);
        }
    }

    public static void main(String[] args) {
        LeetCode0090 leetcode = new LeetCode0090();
        List<List<Integer>> result = leetcode.subsetsWithDup(new int[] { 1, 2, 2 });
        for (int i = 0; i < result.size(); i++) {
            for (int j = 0; j < result.get(i).size(); j++) {
                System.out.print(result.get(i).get(j));
            }
            System.out.println();
        }
    }
}
DFS
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/1486/(转载时请注明本文出处及文章链接)