Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added SubSets/step1.md
Empty file.
19 changes: 19 additions & 0 deletions SubSets/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```java
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これも再帰の仕方が何種類かあるように思います。あとループに直せるといいと思います。

// Time spend: 02:01
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tempList が気になってしまいました。
backtrack() を呼ぶごとに1つの組み合わせとして res に add() しているので、subset とかでもいいかもしれません。
と、すみません backtrack() も気になりました。
makeSubsets() とか、何かしら付けるといいかもしれません。

res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```
59 changes: 59 additions & 0 deletions SubSets/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
```java
// Time spend: 01:41
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```

```java
// Time spend: 01:51
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```

```java
// Time spend: 01:23
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}

private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(res, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
```