Subsets *

Source: Internet
Author: User
Tags addall

Given a set of distinct integers, nums, 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 nums = [1,2,3] , a solution is:

[  3],  [1], [2], [1,3], [+]  ,  [2,3], [up  ],  []]



Algorithm idea: for example {}{1}{2}{1,2} all subsets of {* *} may be assumed to be S1, after adding 3 that is {The full subset of the {? The simple way to do this is to add 3 to the subset of {3}{1,3}{2,3}{1,2,3} to get the new subset {*.} to S2,
S1∩S2 is a full subset of {.


 Public classSolution { Publiclist<list<integer>> Subsets (int[] nums) {List<List<Integer>> re =NewArraylist<list<integer>>(); Re.add (NewArraylist<integer> ());//Initialize, add an empty setArrays.sort (nums);  for(inti:nums) {List<List<Integer>> tmp =NewArraylist<list<integer>>();  for(list<integer>set:re) {List<Integer> Tmp_set =NewArraylist<integer>(); Tmp_set.addall (set);//Clone original Existing listTmp_set.add (i);            Tmp.add (Tmp_set);        } re.addall (TMP); }        returnre; }}

Subsets *

Related Article

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.