From c40c86057ef349c837c07f9a3296ea49d1c16ed6 Mon Sep 17 00:00:00 2001 From: ZHD <13417014227@163.com> Date: Sun, 26 Oct 2025 11:29:22 +0800 Subject: [PATCH] =?UTF-8?q?Update=200078.=E5=AD=90=E9=9B=86.md=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=98=E5=8F=96=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0078.\345\255\220\351\233\206.md" | 36 ++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 844b8dc2ca..bb0865d687 100755 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -176,8 +176,10 @@ public: ## 其他语言版本 - ### Java + +回溯(版本一) + ```java class Solution { List> result = new ArrayList<>();// 存放符合条件结果的集合 @@ -201,7 +203,39 @@ class Solution { } ``` +回溯(版本二) + +```java +/** + * 存取法 + * 参考高中组合的思路,抽象为二叉树 + */ +class Solution { + private List> result = new ArrayList<>(); + private Deque path = new LinkedList<>(); + + public List> subsets(int[] nums) { + backtracking(nums, 0); + return result; + } + + public void backtracking(int[] nums, int startIndex) { + if (startIndex >= nums.length) { + result.add(new ArrayList(path)); + return; + } + // 不放入回溯 + backtracking(nums, startIndex + 1); + // 放入回溯 + path.offerLast(nums[startIndex]); + backtracking(nums, startIndex + 1); + path.pollLast(); + } +} +``` + ### Python + ```python class Solution: def subsets(self, nums):