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

Commit1c147e6

Browse files
Create 90-Subsets-II.java
1 parent6102d45 commit1c147e6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎java/90-Subsets-II.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//Similar to subsets 1. Here, we'll just take care of the duplicates.
2+
//This video was helpful https://www.youtube.com/watch?v=mcg4qKbAmmY&t=316s&ab_channel=Fraz
3+
classSolution {
4+
publicList<List<Integer>>subsetsWithDup(int[]nums) {
5+
Arrays.sort(nums);
6+
List<List<Integer>>ans =newArrayList<>();
7+
ArrayList<Integer>list =newArrayList<>();
8+
helper(ans,0,nums,list,false);
9+
returnans;
10+
}
11+
12+
publicvoidhelper(List<List<Integer>>ans,intstart,int[]nums,List<Integer>list,booleanignored) {
13+
if (start>=nums.length) {
14+
ans.add(newArrayList<>(list));
15+
}else {
16+
helper(ans,start+1,nums,list,true);
17+
//if we've ignored the value earlier then we must ignore all the values after that too
18+
if (start>0 &&nums[start-1]==nums[start] &&ignored)
19+
return;
20+
list.add(nums[start]);
21+
helper(ans,start+1,nums,list,false);
22+
list.remove(list.size()-1);
23+
}
24+
}
25+
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp