From 48f9150b9a04d2105c466e37acab35d578111d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E6=96=87?= <30338551+YiwenXie@users.noreply.github.com> Date: Wed, 12 May 2021 22:42:29 +0900 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200078.=E5=AD=90=E9=9B=86.md?= =?UTF-8?q?=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0078.子集.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index 8c68843d..77854133 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -177,7 +177,33 @@ public: Java: +```java +class Solution { + List> result = new ArrayList<>();// 存放符合条件结果的集合 + LinkedList path = new LinkedList<>();// 用来存放符合条件结果 + public List> subsets(int[] nums) { + if (nums.length == 0){ + result.add(new ArrayList<>()); + return result; + } + Arrays.sort(nums); + subsetsHelper(nums, 0); + return result; + } + private void subsetsHelper(int[] nums, int startIndex){ + result.add(new ArrayList<>(path));//「遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合」。 + if (startIndex >= nums.length){ //终止条件可不加 + return; + } + for (int i = startIndex; i < nums.length; i++){ + path.add(nums[i]); + subsetsHelper(nums, i + 1); + path.removeLast(); + } + } +} +``` Python: