FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc78.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc78.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (35 loc) · 1.09 KB
Breadcrumbs
leetcode
/
code
/
lc78.java
Copy path
File metadata and controls
36 lines (35 loc) · 1.09 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
package
code
;
/*
* 78. Subsets
* 题意:求数组子集
* 难度:Medium
* 分类:Array, Backtracking, Bit Manipulation
* 思路:回溯法
* Tips:和lc46,39作比较.要找出子集,所以每次backtracking直接添加进res
*/
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
lc78
{
public
static
void
main
(
String
[]
args
) {
int
[]
nums
= {
1
,
2
,
3
};
System
.
out
.
println
(
subsets
(
nums
).
toString
());
}
public
static
List
<
List
<
Integer
>>
subsets
(
int
[]
nums
) {
List
<
List
<
Integer
>>
res
=
new
ArrayList
<
List
<
Integer
>>();
res
.
add
(
new
ArrayList
<>());
if
(
nums
.
length
==
0
)
return
res
;
backtracking
(
res
,
nums
,
new
ArrayList
(),-
1
);
return
res
;
}
public
static
void
backtracking
(
List
<
List
<
Integer
>>
res
,
int
[]
nums
,
List
l
,
int
start
){
if
(
start
>=
nums
.
length
)
return
;
for
(
int
i
=
start
+
1
;
i
<
nums
.
length
;
i
++) {
l
.
add
(
nums
[
i
]);
res
.
add
(
new
ArrayList
<
Integer
>(
l
));
backtracking
(
res
,
nums
,
l
,
i
);
l
.
remove
((
Integer
)
nums
[
i
]);
}
}
}
Back
|
FazBrowse Home
|
New Git URL