Java實(shí)現(xiàn)LeetCode(組合總和)
leetcode題目
組合總和 -- leetcode 39
題目描述
給定一個(gè)無重復(fù)元素的數(shù)組 candidates 和一個(gè)目標(biāo)數(shù) target ,
找出 candidates 中所有可以使數(shù)字和為 target 的組合。
candidates 中的數(shù)字可以無限制重復(fù)被選取。
說明:
所有數(shù)字(包括 target)都是正整數(shù)。
解集不能包含重復(fù)的組合。
示例 1:
輸入: candidates = [2,3,6,7], target = 7,
所求解集為:
[
[7],
[2,2,3]
]
示例 2:
輸入: candidates = [2,3,5], target = 8,
所求解集為:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/combination-sum
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
思路
回溯算法
遞歸找和為target的組合,出口為和超過了target
代碼
package com.leetcode.array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 題目: * 組合總和 -- leetcode 39 * * 題目描述: * 給定一個(gè)無重復(fù)元素的數(shù)組 candidates 和一個(gè)目標(biāo)數(shù) target , 找出 candidates 中所有可以使數(shù)字和為 target 的組合。 candidates 中的數(shù)字可以無限制重復(fù)被選取。 說明: 所有數(shù)字(包括 target)都是正整數(shù)。 解集不能包含重復(fù)的組合。 示例 1: 輸入: candidates = [2,3,6,7], target = 7, 所求解集為: [ [7], [2,2,3] ] 示例 2: 輸入: candidates = [2,3,5], target = 8, 所求解集為: [ [2,2,2,2], [2,3,3], [3,5] ] 來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/combination-sum 著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。 */ public class CombinationSum { /** * 思路: * 1、回溯算法 * 2、遞歸找和為target的組合,出口為和超過了target */ public List<List<Integer>> combinationSum(int[] bb, int target) { List<List<Integer>> res = new ArrayList<>(); if (bb == null) { return res; } addCombinations(bb, 0, target, new ArrayList<Integer>(), res); return res; } private void addCombinations(int[] bb, int start, int target, List<Integer> cache, List<List<Integer>> res) { if (target < 0) { return; } if (target == 0) { res.add(new ArrayList<>(cache)); return; } for (int i=start; i<bb.length; i++) { cache.add(bb[i]); addCombinations(bb,i,target-bb[i],cache,res); cache.remove(cache.size()-1); } } /** * 思路: * 優(yōu)化后的回溯 */ public List<List<Integer>> combinationSumII(int[] bb, int target) { List<List<Integer>> res = new ArrayList<>(); if (bb == null) { return res; } // 排序數(shù)組后 可以在遞歸的時(shí)候減少遞歸次數(shù),配合 if (bb[i] > target) break; Arrays.sort(bb); addCombinationsII(bb, 0, target, new ArrayList<Integer>(), res); return res; } private void addCombinationsII(int[] bb, int start, int target, List<Integer> cache, List<List<Integer>> res) { if (target < 0) { return; } if (target == 0) { res.add(new ArrayList<>(cache)); return; } for (int i=start; i<bb.length; i++) { // 配合排序后的數(shù)組 提升性能 if (bb[i] > target) { break; } cache.add(bb[i]); addCombinationsII(bb,i,target-bb[i],cache,res); cache.remove(cache.size()-1); } } }
到此這篇關(guān)于Java實(shí)現(xiàn)LeetCode(組合總和)的文章就介紹到這了,更多相關(guān)Java實(shí)現(xiàn)組合總數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java代碼實(shí)現(xiàn)RocketMQ的生產(chǎn)與消費(fèi)消息
這篇文章介紹一下其他的小組件以及使用Java代碼實(shí)現(xiàn)生產(chǎn)者對消息的生成,消費(fèi)者消費(fèi)消息等知識點(diǎn),并通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07Spark SerializedLambda錯誤的兩種解決方案
這篇文章主要介紹了Spark SerializedLambda錯誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot之如何正確、安全的關(guān)閉服務(wù)
這篇文章主要介紹了SpringBoot之如何正確、安全的關(guān)閉服務(wù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03深入了解SpringAOP中的jdk動態(tài)代理與CGlib
這篇文章主要介紹了深入了解SpringAOP中的jdk動態(tài)代理與CGlib,一般我們編寫程序的思想是縱向的,也就是一個(gè)方法代碼從該方法第一行開始往下一步一步走,直到走完最后一行代碼,也就是說很多業(yè)務(wù)都需要的比如用戶鑒權(quán),資源釋放等,需要的朋友可以參考下2023-12-12java中BCryptPasswordEncoder密碼的加密與驗(yàn)證方式
這篇文章主要介紹了java中BCryptPasswordEncoder密碼的加密與驗(yàn)證方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08