[Leetcode] 039. Combination Sum (Medium) (c + +)

Source: Internet
Author: User

Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode


039. Combination Sum (Medium) links

Title: https://leetcode.com/problems/combination-sum/
Code (GitHub): Https://github.com/illuz/leetcode

Test Instructions

Given a set of positive integers, and a target number, select some numbers from the set so that they are equal to the number of targets and can be repeated to select the number in the collection.
The collection of the resulting solutions cannot have duplicates.

Analysis

Violent search used to be possible, first ordered, and then with DFS, each time there are two options, one is to select the current number and then recursive current number, the second is not to select the current number of direct recursion under the number.
This question is very good for understanding DFS.

Code: (c + +)

/** Author:illuz <iilluzen[at]gmail.com>* file:ac_dfs_n!. cpp* Create date:2015-01-01 10:45:58* Descripton:dfs, choose or not choose*/#include <bits/stdc++.h>using names  Pace Std;const int N = 0;class Solution {private:void dfs (vector<vector<int> > &ans, vector<int>        &single, vector<int> &candi, int cur, int rest) {int sz = candi.size ();        if (sz <= cur | | Rest < 0) return;            if (rest = = 0) {ans.push_back (single);        Return        }//Choose cur single.push_back (candi[cur]);        DFS (ans, single, Candi, cur, rest-candi[cur]);        Single.pop_back ();    Don ' t choose Cur dfs (ans, single, Candi, cur + 1, rest); }public:vector<vector<int> > Combinationsum (vector<int> &candidates, int target) {vector        <vector<int> > ans;        Vector<int> single; Sort (candidates. Begin (), Candidates.end ());        DFS (ans, single, candidates, 0, target);    return ans;    }};int Main () {int tar;    int n;    Solution S;    CIN >> n >> Tar;    Vector<int> v (n);    for (int i = 0; i < n; i++) cin >> V[i];    vector<vector<int> > Res = s.combinationsum (v, tar);        for (auto &i:res) {for (auto &j:i) cout << J << ';    Puts (""); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Leetcode] 039. Combination Sum (Medium) (c + +)

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.