摘要:通過(guò)這一點(diǎn),我們構(gòu)成一個(gè)遞歸表達(dá)式,但是因?yàn)閱渭兊倪f歸表達(dá)式?jīng)]有計(jì)算中間結(jié)果,所以會(huì)造成大量重復(fù)的計(jì)算影響效率,所以這里采用的思路額外的用數(shù)組來(lái)記錄已經(jīng)計(jì)算過(guò)的結(jié)果。比如,如果沒(méi)有,則需要重復(fù)計(jì)算的結(jié)果。
題目要求
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example: nums = [1, 2, 3] target = 4 The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7. Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
有一個(gè)不包含重復(fù)值的正整數(shù)數(shù)組nums,問(wèn)從數(shù)組中選擇幾個(gè)數(shù),其和為target,這樣的數(shù)的組合有幾種?
思路一:自頂向下的dp這題本質(zhì)上需要注意一點(diǎn),就是我如果需要組成target,那么一定是由nums中的一個(gè)值和另一個(gè)值的排列組合結(jié)果構(gòu)成的。比如com[4] = com[4-1] + com[4-2] + com[4-1]。通過(guò)這一點(diǎn),我們構(gòu)成一個(gè)遞歸表達(dá)式,但是因?yàn)閱渭兊倪f歸表達(dá)式?jīng)]有計(jì)算中間結(jié)果,所以會(huì)造成大量重復(fù)的計(jì)算影響效率,所以這里采用dp的思路額外的用數(shù)組來(lái)記錄已經(jīng)計(jì)算過(guò)的com結(jié)果。比如com[3] = com[2] + com[1], com[2] = com[1],如果沒(méi)有dp,則需要重復(fù)計(jì)算com[1]的結(jié)果。
public int combinationSum4(int[] nums, int target) { if (nums == null || nums.length < 1) { return 0; } int[] dp = new int[target + 1]; Arrays.fill(dp, -1); dp[0] = 1; return helper(nums, target, dp); } private int helper(int[] nums, int target, int[] dp) { if (dp[target] != -1) { return dp[target]; } int res = 0; for (int i = 0; i < nums.length; i++) { if (target >= nums[i]) { res += helper(nums, target - nums[i], dp); } } dp[target] = res; return res; }思路二:自底向上dp
public int combinationSum4(int[] nums, int target) { Arrays.sort(nums); int[] combinationCount = new int[target+1]; combinationCount[0] = 1; for(int i = 1 ; i<=target ; i++) { for(int j = 0 ; j
更多技術(shù)資訊,面試教程和互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號(hào)!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/72685.html
摘要:和唯一的不同是組合中不能存在重復(fù)的元素,因此,在遞歸時(shí)將初始位即可。 Combination Sum I Problem 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...
摘要:?jiǎn)未芜x擇最大體積動(dòng)規(guī)經(jīng)典題目,用數(shù)組表示書包空間為的時(shí)候能裝的物品最大容量。注意的空間要給,因?yàn)槲覀円蟮氖堑趥€(gè)值,否則會(huì)拋出。依然是以背包空間為限制條件,所不同的是取的是價(jià)值較大值,而非體積較大值。 Backpack I Problem 單次選擇+最大體積 Given n items with size Ai, an integer m denotes the size of a b...
摘要:此時(shí),若也正好減小為,說(shuō)明當(dāng)前集合是正解,加入數(shù)組。兩個(gè)無(wú)法得到正解的情況是在為,而不為時(shí),當(dāng)然已經(jīng)無(wú)法得到正解,。在不為而卻已經(jīng)小于等于的情況下,此時(shí)仍要加入其它數(shù)以令為,而要加入的數(shù)都是到的正整數(shù),所以已無(wú)法滿足令為的條件,。 Combination Sum I & II: link Combination Sum III Problem Find all possible com...
摘要:題目要求輸入和,找到所有個(gè)不同數(shù)字的組合,這些組合中數(shù)字的和為參考,解答這是一道典型的的題目,通過(guò)遞歸的方式記錄嘗試的節(jié)點(diǎn),如果成功則加入結(jié)果集,如果失敗則返回上一個(gè)嘗試的節(jié)點(diǎn)進(jìn)行下一種嘗試。 題目要求 Find all possible combinations of k numbers that add up to a number n, given that only numbe...
摘要:深度優(yōu)先搜索復(fù)雜度時(shí)間空間遞歸??臻g思路因?yàn)槲覀兛梢匀我饨M合任意多個(gè)數(shù),看其和是否是目標(biāo)數(shù),而且還要返回所有可能的組合,所以我們必須遍歷所有可能性才能求解。這題是非?;厩业湫偷纳疃葍?yōu)先搜索并返回路徑的題。本質(zhì)上是有限深度優(yōu)先搜索。 Combination Sum I Given a set of candidate numbers (C) and a target number (...
閱讀 2001·2021-11-23 09:51
閱讀 1447·2021-11-18 10:02
閱讀 1038·2021-10-25 09:44
閱讀 2171·2019-08-26 18:36
閱讀 1694·2019-08-26 12:17
閱讀 1228·2019-08-26 11:59
閱讀 2805·2019-08-23 15:56
閱讀 3433·2019-08-23 15:05