Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0d09cf3

Browse files
Create 78-Subsets.java
1 parent884b3b9 commit0d09cf3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎java/78-Subsets.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//The idea is to have two conditions: One in which we will take the element into consideration, Second in which we won't take the element into consideration.
2+
//This video was good for code explanation. https://www.youtube.com/watch?v=6BPurabdAl4&t=508s&ab_channel=Fraz
3+
classSolution {
4+
publicList<List<Integer>>subsets(int[]nums) {
5+
List<List<Integer>>ans =newArrayList<>();
6+
List<Integer>list =newArrayList<>();
7+
helper(ans,0,nums,list);
8+
returnans;
9+
}
10+
11+
publicvoidhelper(List<List<Integer>>ans,intstart,int[]nums,List<Integer>list) {
12+
if (start>=nums.length) {
13+
ans.add(newArrayList<>(list));//In java, we will have to add like this otherwise it'll give null as it'll just have the reference instead of actual values.
14+
}else {
15+
//add the element and start the recursive call
16+
list.add(nums[start]);
17+
helper(ans,start+1,nums,list);
18+
//remove the element and do the backtracking call.
19+
list.remove(list.size()-1);
20+
helper(ans,start+1,nums,list);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp