Leetcode Subsets II-----java

Source: Internet
Author: User

Given a collection of integers that might contain duplicates, Nums, return all possible subsets.

Note:the solution set must not contain duplicate subsets.

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

[  2],  [1],  [1,2,2],  [2,2], [up  ],  []]

This problem is the extension of subsets, except that there are repeating numbers in the array.

So every time I meet a duplicate number, continue is good. (Note that flag must be set to have flag, otherwise there will be a duplicate number in the result).

 Public classSolution {List<List<Integer>> res =NewArraylist<>(); int[] num; intLen;  PublicList<list<integer>> Subsetswithdup (int[] nums) {Len=nums.length; Num=Nums;        Arrays.sort (num); Res.add (NewArraylist<>()); Sub (0,NewArraylist<>() ); returnRes; }     Public voidSubintStart,list<integer>ans) {        intFlag = 0;  for(inti = start;i<len;i++){            if(i > 0 && flag = = 1 && num[i] = = Num[i-1])                Continue;            Ans.add (Num[i]); Flag= 1; Res.add (NewArraylist<integer>(ans)); Sub (i+1, ans); Ans.remove (Ans.size ()-1); }        return ; }}

Leetcode Subsets II-----java

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.