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

Commitc5edc96

Browse files
author
applewjg
committed
SubsetsI && II
Change-Id: I67bb9397e76032e758cf3910de40e29f5cbaac2d
1 parent000a7c5 commitc5edc96

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

‎Subsets.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Nov 18, 2014
4+
Problem: Subsets
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/subsets/
7+
Notes:
8+
Given a set of distinct integers, S, return all possible subsets.
9+
Note:
10+
Elements in a subset must be in non-descending order.
11+
The solution set must not contain duplicate subsets.
12+
For example,
13+
If S = [1,2,3], a solution is:
14+
[
15+
[3],
16+
[1],
17+
[2],
18+
[1,2,3],
19+
[1,3],
20+
[2,3],
21+
[1,2],
22+
[]
23+
]
24+
25+
Solution: 1. Updated Iterative solution.
26+
2. Updated Recursive solution.
27+
*/
28+
publicclassSolution {
29+
publicList<List<Integer>>subsets(int[]S) {
30+
returnsubsets_2(S);
31+
}
32+
publicList<List<Integer>>subsets_1(int[]S) {
33+
Arrays.sort(S);
34+
List<List<Integer>>res =newArrayList<List<Integer>>();
35+
List<Integer>path =newArrayList<Integer>();
36+
subsetsRe(S,0,path,res);
37+
returnres;
38+
}
39+
voidsubsetsRe(int[]S,intstart,List<Integer>path,List<List<Integer>>res) {
40+
List<Integer>sub =newArrayList<Integer>(path);
41+
res.add(sub);
42+
for (inti =start;i <S.length; ++i) {
43+
path.add(S[i]);
44+
subsetsRe(S,i +1,path,res);
45+
path.remove(path.size() -1);
46+
}
47+
}
48+
publicList<List<Integer>>subsets_2(int[]S) {
49+
Arrays.sort(S);
50+
List<List<Integer>>res =newArrayList<List<Integer>>();
51+
res.add(newArrayList<Integer>());
52+
for (inti =0;i <S.length; ++i) {
53+
intsz =res.size();
54+
for (intj =0;j <sz; ++j) {
55+
List<Integer>path =newArrayList<Integer>(res.get(j));
56+
path.add(S[i]);
57+
res.add(path);
58+
}
59+
}
60+
returnres;
61+
}
62+
}

‎SubsetsII.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Nov 18, 2014
4+
Problem: Subsets II
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/subsets-ii/
7+
Notes:
8+
Given a collection of integers that might contain duplicates, S, return all possible subsets.
9+
Note:
10+
Elements in a subset must be in non-descending order.
11+
The solution set must not contain duplicate subsets.
12+
For example,
13+
If S = [1,2,2], a solution is:
14+
[
15+
[2],
16+
[1],
17+
[1,2,2],
18+
[2,2],
19+
[1,2],
20+
[]
21+
]
22+
23+
Solution: ..Similar to Subset I.
24+
*/
25+
publicclassSolution {
26+
publicList<List<Integer>>subsetsWithDup(int[]S) {
27+
returnsubsetsWithDup_2(S);
28+
}
29+
publicList<List<Integer>>subsetsWithDup_1(int[]S) {
30+
Arrays.sort(S);
31+
List<List<Integer>>res =newArrayList<List<Integer>>();
32+
List<Integer>path =newArrayList<Integer>();
33+
subsetsRe(S,0,path,res);
34+
returnres;
35+
}
36+
voidsubsetsRe(int[]S,intstart,List<Integer>path,List<List<Integer>>res) {
37+
List<Integer>sub =newArrayList<Integer>(path);
38+
res.add(sub);
39+
for (inti =start;i <S.length; ++i) {
40+
if (i !=start &&S[i] ==S[i-1])continue;
41+
path.add(S[i]);
42+
subsetsRe(S,i +1,path,res);
43+
path.remove(path.size() -1);
44+
}
45+
}
46+
publicList<List<Integer>>subsetsWithDup_2(int[]S) {
47+
Arrays.sort(S);
48+
List<List<Integer>>res =newArrayList<List<Integer>>();
49+
res.add(newArrayList<Integer>());
50+
intpresz =0;
51+
for (inti =0;i <S.length; ++i) {
52+
intsz =res.size();
53+
for (intj =0;j <sz; ++j) {
54+
if (i ==0 ||S[i] !=S[i-1] ||j >=presz) {
55+
List<Integer>path =newArrayList<Integer>(res.get(j));
56+
path.add(S[i]);
57+
res.add(path);
58+
}
59+
}
60+
presz =sz;
61+
}
62+
returnres;
63+
}
64+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp