1. 程式人生 > >39. Combination Sum (重要)

39. Combination Sum (重要)

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7

A solution set is: 

[
  [7],
  [2, 2, 3]
]
class Solution {
public:
	vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
		vector<int>tmp;
		dfs(candidates, target, tmp, 0);
		return s;
	}
private:

	void dfs(vector<int>& candidates, int target, vector<int>&tmp,int cur){
		if (target < 0) return;
		if (target==0){
			s.push_back(tmp);
			return;
		}
		for (int i = cur; i < candidates.size(); i++){
			tmp.push_back(candidates[i]);
			dfs(candidates, target-candidates[i], tmp,i);//i,因為一個數字可以出現多次
			tmp.pop_back();
		}
	}

	vector<vector<int> > s;
};

class Solution {
public:
	vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
		sort(candidates.begin(), candidates.end());
		vector<int>tmp;
		dfs(candidates, target, tmp, 0);
		return s;
	}
private:

	void dfs(vector<int>& candidates, int target, vector<int>&tmp,int cur){
		if (target < 0) return;
		if (target==0){
			s.push_back(tmp);
			return;
		}
		for (int i = cur; i < candidates.size(); i++){
			if (cur != i&&candidates[i] == candidates[i-1]) continue;
			tmp.push_back(candidates[i]);
			dfs(candidates, target-candidates[i], tmp,i);
			tmp.pop_back();
		}
	}

	vector<vector<int> > s;
};


相關推薦

39. Combination Sum 重要

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C whe

【LeetCode】39. Combination SumC++

地址:https://leetcode.com/problems/combination-sum/ 題目: Given a set of candidate numbers (candidates) (without duplicates) and a target number (

LeetCode 39. Combination Sum組合總數

原題 Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in

LeetCode 39. 組合總和 Combination SumC語言

題目描述: 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重複被選取。 說明: 所有數字(包括 target)都是正整數。

39. Combination Sum 40 Combination Sum 2Array, DFS

39. Combination Sum Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in

LeetCode第39題之Combination Sum兩種方法

思路:兩種方法都是利用遞歸回溯,第二方法在第一種方法的基礎上對原始資料先進行排序,這樣可以剪枝,加快計算速度。第一種方法在LeetCode上測試執行的時間是24ms,第二種方法執行時間為16ms。 方

LeetCode 39. Combination Sum 時間複雜度O( n^k​​​​​​​)

時間複雜度(O( n^k)),思想,DFS 深度遍歷 class Solution { public: vector<vector<int>> combinationSum(vector<int>& candidates,

29.Combination Sum和為sum的組合

剪枝 pos clas java rep array ret turn mov Level: ??Medium 題目描述: Given a set of candidate numbers (candidates) (without duplicates) and a ta

[UVALive7261]A - Xiongnu&#39;s Land 二分

while continue 大於 並且 輸出結果 net lan include != 題目鏈接:https://vjudge.net/problem/UVALive-7261 題意略 三個步驟: 1.二分滿足左邊綠洲面積大於等於右邊綠洲面積,並且使左邊面積盡可能大的分割

HDU 4415 Assassin&#39;s Creed貪心

all ria put space clas name 它的 tor problem pid=4415">HDU 4415 題意: 壯哉我Assassin! E叔有一柄耐久度為m的袖劍,以及n個目標士兵要去解決。 每解決掉一個士兵,消耗袖劍Ai的

POJ 3069 Saruman&#39;s Army 貪心

依次 至少 一個 ide mage != cnblogs style man 題目大意:直線上有N個點,點i的位置是Xi,從這N個點中選取若幹,給他們加上標記,對每一個點,其距離為R以內的區域內必須有被標記的點。求至少需要多少個點被標記。 題目思路:設最左邊的點:點p的

POJ 3128 Leonardo&#39;s Notebook 置換

gif telling freopen for each align lock 需要 text page Leonardo‘s Notebook Time Limit: 1000MS Memory Limit: 65536K Total Submission

A Knight&#39;s Journey DFS

rpe for pos sca board around span this osi Background The knight is getting bored of seeing the same black and white squares again and a

HDU 6043 KazaQ&#39;s Socks 規律

n-1 cnblogs sample swe 順序 裏的 this c-s close Description KazaQ wears socks everyday. At the beginning, he has nn pairs of socks numbered

String類中常用的方法重要

循環 類型 demo width 尋找 str2 子字符串 replace table 1.字符串與字節 public String(byte[] byte); 將全部字節變成字符串 public String (byte[] byte,int offset,in

leetcode 39. Combination Sum

pop and highlight tar 如果 pub logs int 每一個 類似 二叉樹遍歷到根節點path,也類似深度優先搜索 首先保持一個result的vector<vector>,是引用類型。 聲明一個保持中間結果的vector,這個vec

LibreOJ #6220. sum數論

define onclick const 全部 註意 img long long %d mes   題目大意:在數組中找出一些數,使它們的和能被n整除   這題標簽是數學,那我就標題就寫數論好了...   顯然如果數組中有n的倍數直接取就行。   那假設數組中沒有n的

bzoj1257: [CQOI2007]余數之和sum數論

img const ima left class -1 找到 一段 max   非常經典的題目...   要求      則有   實際上   最多只有2*sqrt(k)種取值,非常好證明   因為>=sqrt(k)的數除k下取整得到的數一定<=sq

leecode-39. Combination Sum

limited mit blank 列表 mov rep 空間復雜度 cas () 1、問題描述: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find

Leetcode 39: Combination Sum

back first eth mit mes which ray 100% length Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all