From a31b5865df04e5004724995e2b36610ec45d9489 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 10 Jul 2023 16:00:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E5=90=88=E6=96=B0=E5=A2=9Ejava?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9C=AA=E5=89=AA=E6=9E=9D=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 444e15ce..8ade6e11 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -351,7 +351,30 @@ public: ### Java: +未剪枝优化 +```java +class Solution { + List> result= new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combine(int n, int k) { + backtracking(n,k,1); + return result; + } + public void backtracking(int n,int k,int startIndex){ + if (path.size() == k){ + result.add(new ArrayList<>(path)); + return; + } + for (int i =startIndex;i<=n;i++){ + path.add(i); + backtracking(n,k,i+1); + path.removeLast(); + } + } +} +``` +剪枝优化: ```java class Solution { List> result = new ArrayList<>();