From 29e1a07cc3a51cb61c5aec933d5d914eb105741d Mon Sep 17 00:00:00 2001 From: sharky <1821984081@qq.com> Date: Fri, 17 Mar 2023 12:22:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E7=AE=97=E6=B3=95=E5=8E=BB?= =?UTF-8?q?=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6=E4=B8=80=E7=A7=8D?= =?UTF-8?q?=E5=86=99=E6=B3=95=EF=BC=8C40.=E7=BB=84=E5=90=88=E6=80=BB?= =?UTF-8?q?=E5=92=8CII=E7=9A=84java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\347\247\215\345\206\231\346\263\225.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index b3bd74da28..cbbd831f8f 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -317,7 +317,38 @@ class Solution { } } ``` +**40.组合总和II** +```java +class Solution { + List> result = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort( candidates ); + if( candidates[0] > target ) return result; + backtracking(candidates,target,0,0); + return result; + } + public void backtracking(int[] candidates,int target,int sum,int startIndex){ + if( sum > target )return; + if( sum == target ){ + result.add( new ArrayList<>(path) ); + } + HashSet hashSet = new HashSet<>(); + for( int i = startIndex; i < candidates.length; i++){ + if( hashSet.contains(candidates[i]) ){ + continue; + } + hashSet.add(candidates[i]); + path.add(candidates[i]); + sum += candidates[i]; + backtracking(candidates,target,sum,i+1); + path.removeLast(); + sum -= candidates[i]; + } + } +} +``` Python: **90.子集II**