FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/problems/src/backtracking/SubsetsII.java at master · KindleBooks66/leetcode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
KindleBooks66
/
leetcode
Public
forked from
gouthampradhan/leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
problems
/
src
/
backtracking
/
SubsetsII.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (45 loc) · 1.28 KB
Breadcrumbs
leetcode
/
problems
/
src
/
backtracking
/
SubsetsII.java
Copy path
File metadata and controls
48 lines (45 loc) · 1.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package
backtracking
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
/**
* Created by gouthamvidyapradhan on 14/03/2017. Given a collection of integers that might contain
* duplicates, nums, return all possible subsets.
*
* <p>Note: The solution set must not contain duplicate subsets.
*
* <p>For example, If nums = [1,2,2], a solution is:
*
* <p>[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
*/
public
class
SubsetsII
{
/**
* Main method
*
* @param args
* @throws Exception
*/
public
static
void
main
(
String
[]
args
)
throws
Exception
{
int
[]
n
= {
1
,
2
,
3
};
List
<
List
<
Integer
>>
result
=
new
SubsetsII
().
subsetsWithDup
(
n
);
}
public
List
<
List
<
Integer
>>
subsetsWithDup
(
int
[]
nums
) {
List
<
List
<
Integer
>>
result
=
new
ArrayList
<>();
result
.
add
(
new
ArrayList
<>());
// empty subset
int
start
=
0
,
newStart
=
0
;
Arrays
.
sort
(
nums
);
for
(
int
i
=
0
,
l
=
nums
.
length
;
i
<
l
;
i
++) {
newStart
=
result
.
size
();
if
(
i
==
0
||
nums
[
i
] !=
nums
[
i
-
1
]) {
start
=
0
;
}
for
(
int
j
=
start
,
resLen
=
result
.
size
();
j
<
resLen
;
j
++) {
List
<
Integer
>
newList
=
new
ArrayList
<>(
result
.
get
(
j
));
newList
.
add
(
nums
[
i
]);
result
.
add
(
newList
);
}
start
=
newStart
;
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL