[LeetCode] 039. Combination Sum (Medium) (C ++), combinationleetcode

Source: Internet
Author: User

[LeetCode] 039. Combination Sum (Medium) (C ++), combinationleetcode

Index: [LeetCode] Leetcode index (C ++/Java/Python/SQL)
Github: https://github.com/illuz/leetcode


039. Combination Sum (Medium) Link:

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

Question:

A positive integer set and a target number are given. You can select a number from the set so that their sum is equal to the target number.
The resulting solution set cannot be repeated.

Analysis:

Brute-force search is acceptable in the past. First, sort the order and then use DFS. There are two options each time. One is to select the current number and then recursion the current number, second, if the current number is not selected, the current number is recursive.
It is good to understand 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 namespace 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.