摘要:給定一個(gè)正整數(shù)數(shù)組元素?zé)o重復(fù),給定目標(biāo),找出組合的個(gè)數(shù),使得組合中元素的和等于。數(shù)組元素可以重復(fù)使用遞歸調(diào)用循環(huán)中,對(duì)于第一題修改的起始位置即可但是。結(jié)果如果第一處修改成結(jié)果變?yōu)槿绻谝惶幮薷臑橐约暗诙幮薷臑榻Y(jié)果變?yōu)?/p>
Combination Sum I
Given a set of candidate numbers (C) (without duplicates) 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.
例如: [2, 3, 6, 7] and target 7
[ [7], [2, 2, 3] ]
給定一個(gè)數(shù)組(元素?zé)o重復(fù)),和一個(gè)目標(biāo)值,找到所有組合,加起來(lái)等于目標(biāo)值。數(shù)組中的元素可以重復(fù)使用.
解法:
public class CombinationSum { public static ListLC40. Combination Sum II> combinationSum(int[] candidates, int target){ Arrays.sort(candidates); List
> result = new ArrayList<>(); getResult(result, new ArrayList
(), candidates, target, 0); return result; } private static void getResult(List > result, ArrayList
current, int[] candidates, int target, int start) { if(target<0) return; if(target==0){ result.add(new ArrayList<>(current)); return; } for(int i = start; i = candidates[i]; i++){ current.add(candidates[i]); getResult(result, current, candidates, target-candidates[i], i); current.remove(current.size() - 1); } } public static void main(String[] args) { int[] nums = {2,3,6,7}; System.out.println(combinationSum(nums, 7)); } }
給定一個(gè)數(shù)組(元素可以有重復(fù)),和一個(gè)目標(biāo)值,找到所有組合,加起來(lái)等于目標(biāo)值。數(shù)組中的元素不能重復(fù)使用。
例子: [10, 1, 2, 7, 6, 1, 5] and target 8
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
解法:
/** * 要去重,注意邊界,遞歸時(shí)候要加一 */ public class CombinationSum2 { public ListLC216. Combination Sum III> combinationSum2(int[] candidates, int target) { List
> result = new ArrayList<>(); Arrays.sort(candidates); dfs(result, new ArrayList
(), candidates, target, 0); return result; } private void dfs(List > result, ArrayList
current, int[] candidates, int target, int start) { if(target < 0) return; if(target == 0){ result.add(new ArrayList (current)); return; } for(int i = start; i = candidates[i]; i++){ current.add(candidates[i]); dfs(result, current, candidates, target - candidates[i], i+1); // 此處注意i+1,每個(gè)元素只能用一次,加一后在向下遞歸 current.remove(current.size()-1); while(i < candidates.length - 1 && candidates[i] == candidates[i+1]) i++; // 去重復(fù)(注意上面有i+1,這里要length-1,邊界問(wèn)題) } } public static void main(String[] args) { int[] array = {10, 1, 2, 7, 6, 1, 5}; int target = 8; System.out.println(new CombinationSum2().combinationSum2(array, target)); } }
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Example 1: Input: k = 3, n = 7
Output: [[1,2,4]]
Example 2: Input: k = 3, n = 9
Output: [[1,2,6], [1,3,5], [2,3,4]]
給定K和N,從1--9中這幾個(gè)9個(gè)數(shù)字組合出來(lái)K個(gè)數(shù),其和為N。1-9不能重復(fù)使用.
/** * 注意結(jié)束條件:size達(dá)到k值 并且 剩余值為0 */ public class CombinationSum3 { public ListLC 377. Combination Sum IV> combinationSum3(int k, int n) { List
> result = new ArrayList<>(); dfs(result, new ArrayList
(), k, n, 1); return result; } private void dfs(List > result, ArrayList
current, int k, int remainder, int start){ if(current.size() == k && remainder == 0){ //size達(dá)到k值 并且 剩余值為0 result.add(new ArrayList<>(current)); return ; } for(int i = start; i<=9 && remainder >= i; i++){ current.add(i); dfs(result, current, k, remainder - i, i+1); // 不重復(fù),i+1 current.remove(current.size() - 1); } } public static void main(String[] args) { System.out.println(new CombinationSum3().combinationSum3(3, 15)); } }
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?如果有負(fù)數(shù),就不能讓數(shù)組中的元素重復(fù)使用。
給定一個(gè)正整數(shù)數(shù)組(元素?zé)o重復(fù)),給定目標(biāo)target,找出組合的個(gè)數(shù),使得組合中元素的和等于target。數(shù)組元素可以重復(fù)使用.
public class CombinationSum4 { public int combinationSum4(int[] candidates, int target){ List> result = new ArrayList<>(); dfs(result, new ArrayList
(), candidates, target, 0); return result.size(); } private void dfs(List > result, ArrayList
current, int[] candidates, int target, int start) { if(target < 0) return; if(target == 0){ result.add(new ArrayList<>(current)); return; } for(int i = 0; i = candidates[i]; i++){ current.add(candidates[i]); dfs(result, current, candidates, target-candidates[i], i); current.remove(current.size() - 1); } } public static void main(String[] args) { int[] arr = {1,2,3}; System.out.println(new CombinationSum4().combinationSum4(arr, 4)); } }
遞歸調(diào)用循環(huán)中,對(duì)于第一題修改i的起始位置即可:i = 0
但是TLE。遞歸深度太深。
所以這個(gè)方法是不行的。
需要使用DP。
public int combinationSum4(int[] candidates, int target){ Arrays.sort(candidates); int[] dp = new int[target + 1]; dp[0] = 1; for(int i = 1; i面試題:修改版i) break; dp[i] += dp[i - curr]; } } return dp[target]; }
有道面經(jīng)題目是一個(gè)修改版,也是返回組合個(gè)數(shù)即可,但是加了條件:去掉重復(fù)。
上面的例子:nums = [1, 2, 3] target = 4 ,返回 7.
The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1)
這個(gè)題目要返回的是4,所有的組合是:(1, 1, 1, 1) (1, 1, 2) (1, 3) (2, 2) (3, 1)
變成第一題了:需要改變返回值,返回大小即可。
看一下這幾個(gè)的區(qū)別,輕微的改動(dòng),產(chǎn)生的不同結(jié)果:
以第一題Combination Sum I為基礎(chǔ):
public class CombinationSum { public static List> combinationSum(int[] candidates, int target){ Arrays.sort(candidates); List
> result = new ArrayList<>(); getResult(result, new ArrayList
(), candidates, target, 0); return result; } private static void getResult(List > result, ArrayList
current, int[] candidates, int target, int start) { if(target < 0) return; // 是有可能小于0的 if(target == 0){ result.add(new ArrayList<>(current)); // 此處注意 return; } // 注意點(diǎn)1 for(int i = start; i = candidates[i]; i++){ current.add(candidates[i]); getResult(result, current, candidates, target-candidates[i], i); // 注意點(diǎn)2 current.remove(current.size() - 1); } } public static void main(String[] args) { int[] nums = {1,2,3}; System.out.println(combinationSum(nums, 4)); } }
在上面的兩個(gè)注意點(diǎn)上:
第一題:數(shù)組(元素?zé)o重復(fù)),數(shù)組中的元素可以重復(fù)使用。結(jié)果
[[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2]]
如果第一處修改成 i = 0 結(jié)果變?yōu)椋?/p>
[[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 3], [2, 1, 1], [2, 2], [3, 1]]
如果第一處修改為 i = start 以及 第二處修改為 i+1 結(jié)果變?yōu)椋?/p>
[[1, 3]]
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66761.html
摘要:輸入輸出分析題目由于我們需要找到多個(gè)組合,簡(jiǎn)單的使用循環(huán)肯定是不行的,這時(shí)候我們可以使用回溯算法來(lái)解決這個(gè)問(wèn)題。用回溯算法解決問(wèn)題的一般步驟針對(duì)所給問(wèn)題,定義問(wèn)題的解空間,它至少包含問(wèn)題的一個(gè)最優(yōu)解。 題目描述 Given a set of candidate numbers (candidates) (without duplicates) and a target number ...
摘要:前言從開(kāi)始寫(xiě)相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒(méi)有按順序?qū)懍F(xiàn)在翻起來(lái)覺(jué)得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個(gè)索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開(kāi)始寫(xiě)leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒(méi)有按順序?qū)憽F(xiàn)在翻起來(lái)覺(jué)得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個(gè)索引嘻嘻。 順序整理 1~50 1...
摘要:此時(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...
摘要:希望能夠幫助到大家,減少在起步階段的油耗,集中精神突破技術(shù)。關(guān)注公眾后,后臺(tái)回復(fù)關(guān)鍵字資料,獲取項(xiàng)目包本篇文章對(duì)不同階段的人群都適用,別再說(shuō)怎么學(xué),沒(méi)有實(shí)戰(zhàn)項(xiàng)目了。 showImg(https://segmentfault.com/img/bVbpcg3?w=1318&h=730); 這周應(yīng)該有不少學(xué)校已經(jīng)開(kāi)學(xué)了,那么同學(xué)們都該動(dòng)起來(lái)了,把家里面的那些懶習(xí)慣給扔掉了可以。 不知怎么的,...
閱讀 3224·2021-11-12 10:36
閱讀 1296·2019-08-30 15:56
閱讀 2453·2019-08-30 11:26
閱讀 562·2019-08-29 13:00
閱讀 3621·2019-08-28 18:08
閱讀 2760·2019-08-26 17:18
閱讀 1911·2019-08-26 13:26
閱讀 2440·2019-08-26 11:39