Written question 52. Leetcode OJ (39)

Source: Internet
Author: User

If you remember it or the person who wrote Leetcode will know that this is nsum, it is no longer the twosum,threesum,foursum of the previous. Before the use of the I-layer loop to solve the problem, then the N-layer loop theory can be solved (in fact, it should not), but N is not sure, and is variable, its range is [1,n]

Say here actually want to explain is, we want to change a kind of thinking to solve a problem. The idea that I thought of when I saw this question was:

We are from small to large one will be the smallest number of repeatedly added to the vector, if the vector existing values and larger than target will be the vector set of elements to reduce one, adding the following elements continue to compare, using or backtracking method. Recursive plus backtracking, like this, must finally be able to find all the solution, in fact, there is also a point to note, is to judge the size of the elements in the vector and the target relationship, we do not directly to the vector of all the elements and find out, but using a clever way, the ' target- The update value of Val ' as target is passed to the loop (where Val refers to the value added to the vector), and finally the target is definitely reduced to 0, which is really the value we want to find and add it to the final result. It is important to note that target cannot be passed by reference

The above explanation may make you see some do not understand, but hope the following code can help you understand, in fact, once the idea is clear you can solve the problem, the code is as follows:

Class Solution {public:vector<vector<int>> Combinationsum (vector<int>& candidates, int target) {/* At the beginning of the first eye of the topic, I thought of this topic and the previous mentioned twosum,threesum,foursum different, the subject may be nsum, so n-layer cycle of the practice is not advisable, I think of whether it can        Backtracking: Constantly adding elements, to test, if the element and target equal to meet the test instructions, if the target is greater than the launch cycle, if less than the test can continue.        */vector<vector<int>> ret;        Ret.clear ();        vector<int> tmp;        Tmp.clear ();        The title does not indicate that candidates is orderly, but the result of the title requirement is that there is no repetition of the set, then we need to sort the candidates (Candidates.begin (), Candidates.end ());        The _findallsolve (candidates,ret,tmp,target,0) is solved by recursion and backtracking method.    return ret; } void _findallsolve (vector<int>& candidates, vector<vector<int>>& ret,vector<int>& Amp            Tmp,int target, int index) {if (target = = 0) {//At this point the set in TMP satisfies test instructions, adding to the RET ret.push_back (TMP);        return; } else {for (int i = index; i < candidates.Size (); ++i) {if (Candidates[i] > target) {//title indicates that the elements in candidates are non-negative and candidates are sorted                , you can exit the return directly at this time;                }//target > Candidates[i] Then we will add candidates[i] to TMP Tmp.push_back (Candidates[i]);                _findallsolve (Candidates,ret,tmp,target-candidates[i],i);            After completing the above process, we need to remove them in time and see if there are other solutions without tmp.pop_back (); }        }    }};
The results of the program are as follows:


Written question 52. Leetcode OJ (39)

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.