254. Factor Combinations

Source: Internet
Author: User

It's backtracking.

1      PublicList<list<integer>> Getfactors (intN) {2list<list<integer>> res =NewArraylist<list<integer>>();3Helper (res,NewArraylist<integer> (), N, 2);4         returnRes;5     }6     7     Private voidHelper (list<list<integer>> res, list<integer> item,intNintstart) {8          for(inti = start; I * I <= N; i++) {9             if(n% i = = 0) {Ten Item.add (i); OneList<integer> Curcomb =NewArraylist<integer>(item); ACurcomb.add (N/i); - Res.add (curcomb); -Helper (res, item, N/I, i); theItem.remove (Item.size ()-1); -             } -         } -}

It's important to note that

The helper function needs to have an int start record starting point, otherwise such as decomposition 12 when there will be a repetition, [2,2,3],[3,2,2] will become two groups add in, so each time decomposition can not be greater than the last factor

254. Factor Combinations

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.