[Leetcode 90] Subsets II

Source: Internet
Author: User

Title Link: subsets-ii


Import Java.util.arraylist;import java.util.arrays;import Java.util.hashset;import Java.util.list;import java.util.set;/** * Given A collection of integers that might contain duplicates, S, return all possible subsets. Note:elements in a subset must is in non-descending order. The solution set must not contain duplicate subsets. for example,if S = [1,2,2], a solution is:[[2], [1], [1,2,2], [2,2], [up], []] * */public class Subsetsii {//19/ Test cases passed.//status:accepted//runtime:259 ms//submitted:0 minutes ago//time complexity O (2^n) space complexity O (n)//lazy, direct use of Set container , remove duplicate subset public set<list<integer>> subsets = new hashset<list<integer>> ();p ublic List<List        <Integer>> subsetswithdup (int[] num) {arrays.sort (num);        Subsets (NUM, 0, New arraylist<integer> ());          return new arraylist<list<integer>> (subsets);        } public void subsets (int[] S, int step, list<integer> subset) {if (step = = s.length) {Subsets.add (subset);        Return                }//num[step] does not join the sub-set subsets (S, step + 1, new arraylist<integer> (subset));                Num[step] Join sub-centralized Subset.add (S[step]);            Subsets (S, step + 1, new arraylist<integer> (subset)); } public static void Main (string[] args) {//TODO auto-generated method stub}}


[Leetcode 90] Subsets II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.