Leetcode——組合總和
- 用深信服的一個問題來引出此題:
題目描述: 給定一堆題目所對應的分值,請你從中挑選一些題目,使其分值正好是一百分
解題思路:
- 將分值一個個加到一個列表中,一旦找到就輸出列表
- 否則將所有元素加入列表後,將其回溯,加入下一個元素
- 可以在加入元素大於所要的100以後,就回溯
import java.util.List; import java.util.ArrayList; /*給定一堆題目所對應的分值,請你從中挑選一些題目,使其分值正好是一百分 * coded by Jerome */ public class demo1 { public static List<Integer> list = new ArrayList<>(); public static List<Integer> backtrack(int[] a,int target,int i){ while(i<a.length){ list.add(a[i]); if(sum(list) == target) System.out.println(list); i++; backtrack(a, target, i); list.remove(list.size()-1); } return list; } public static int sum(List<Integer> list){ int sum = 0; for(int x:list) sum += x; return sum; } public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {10,20,80,70}; int target = 100; backtrack(a,target,0); //System.out.println(list); } }
-
leetcode上的這題和上面這題的區別,其實就在於對於每個數字可以重複選擇
- 同時對於回溯的條件作了優化,一旦超過給定值,就回溯
題目描述:給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。candidates 中的數字可以無限制重複被選取。
說明:
- 所有數字(包括 target)都是正整數。
- 解集不能包含重複的組合。
解題思路:
- 從array 裡找到所有的組合,它的和是等於target的。任何組合只要是它的和等於target的話,都需要找到,但是不需要重複的。這道題中是可以重複利用一個數字的,那麼我們就需要每次都代入同一個數字,直到它之和達到target 或者它超過了target, 然後在倒退回去一個數字,繼續找下一個數字,這種情況肯定是要用遞迴了。這裡是backtracking,每次倒退回一個數字,需要繼續遞迴下去,在倒退,一直重複直到搜尋了所有的可能性。
- 舉個例子分析
[2,3,6,7] target 7
2 選2,sum = 2
2+2 選2,sum = 4
2+2+2 選2,sum = 6
2+2+2+2 選2,sum = 8 這時候 sum已經超出target,需要返回到上一個數字
2+2+2+3 選3,sum = 9, 也超出了target, 這裡我們可以發現,如果是sorted array的話,從小到大,只要一 次超出,後面的數字必然也會超出target,所以可以在第一次超出的時候就直接跳出這一個迭代
2+2+3 選3,sum = 7,等於target, 此時返回並且跳出這一個迭代,因為後面的數字肯定超出(array裡不 會有重複的數字)
2+3 選3,sum = 5,小於target,繼續遞迴同一個數字
2+3+3 選3,sum = 8,超出target,返回上一個數字
2+6 選6,sum = 8,超出target,返回上一個數字
3 選3,這裡繼續從3開始遞迴
...
...
...
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(candidates);
backtrack(list,new ArrayList<>(),candidates,target,0);
return list;
}
public static boolean backtrack(List<List<Integer>> list,List<Integer> temp,int[] a,int remain,int start){
if(remain < 0)
return false;
if(remain == 0){
list.add(new ArrayList<Integer>(temp));
return false;
}
else{
for(int i = start;i<a.length;i++){
boolean flag = true;
temp.add(a[i]);
flag = backtrack(list, temp, a, remain-a[i], i);
temp.remove(temp.size()-1);// not i+1,because we can use same number
if(!flag)
break;
}
return true;
}
}
}
相關推薦
Leetcode ---- 組合總和
題目: 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重
LeetCode 組合總和 II 重點整理回溯法
給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 說明: 所有數字(包括目標數)都是正整數。 解集不能包含重複的組合
Leetcode——組合總和
用深信服的一個問題來引出此題: 題目描述: 給定一堆題目所對應的分值,請你從中挑選一些題目,使其分值正好是一百分 解題思路: 將分值一個個加到一個列表中,一旦找到就輸出列表 否則將所有元素加入列表後,將其回溯,加入下一個元素 可以在加入元素大於所要的100以後,就回溯
LeetCode 39. 組合總和(Combination Sum)
gin -s ati div span i++ 不能 ida 思路 題目描述 給定一個無重復元素的數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以
LeetCode 39 40 組合總和 組合總和II (回溯)
1.組合總和 難度:中等 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重複被選取。 說明: 所有數字(包括 target)都是正整數
Leetcode學習筆記 39 組合總和
轉載自:https://www.unclegem.cn/2018/09/10/Leetcode學習筆記-39-組合總和/ 題目: 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 c
leetcode------39--組合總和
給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重複被選取。
Leetcode 40 組合總和 II
題目 給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 說明: 所有數字(包括目標數)都是正整數。 解集不能包含重複的組合。
leetcode題庫——組合總和II
題目描述: 給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中
leetcode題庫——組合總和
題目描述: 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以
Leetcode篇:組合總和
@author: ZZQ @software: PyCharm @file: combinationSum.py @time: 2018/11/14 18:23 要求:給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 targe
Leetcode篇:組合總和II
@author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合
LeetCode 216.組合總和 Ⅲ(C++)
找出所有相加之和為 n 的 k 個數的組合。組合中只允許含有 1 - 9 的正整數,並且每種組合中不存在重複的數字。 說明: 所有數字都是正整數。 解集不能包含重複的組合。 示例 1: 輸入: k =
【LeetCode】216. 組合總和 III 結題報告 (C++)
原題地址:https://leetcode-cn.com/problems/combination-sum-iii/submissions/ 題目描述: 找出所有相加之和為 n 的 k 個數的組合。組合中只允許含有 1 - 9 的正整數,並且每種組合中不存在重複的數字。 說明: 所有
leetcode python 39. 組合總和(中等,陣列,遞迴)
給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重複被選取。 說明:所有數字(包括 target)都是正整數。解集不能包含重複的組合。 示例 1
Leetcode 377:組合總和 Ⅳ(最詳細的解法!!!)
給定一個由正整陣列成且不存在重複數字的陣列,找出和為給定目標正整數的組合的個數。 示例: nums = [1, 2, 3] target = 4 所有可能的組合為: (1, 1, 1, 1) (1,
leetcode刷題之旅(40)組合總和2
題目描述 給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 說明: 所有數字(包括目標數)都是正整數。 解集不能包
leetcode:組合總和II(回溯java)
package LeetCode; import java.util.*; /* 給定一個數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 說明: 所有
leetcode:組合總和(java回溯)
package LeetCode; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /* 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidate
LeetCode筆記——39組合總和
題目: 給定一個無重複元素的陣列 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重複被選取。 說明: 所有數字(包括 target)都是正整數。