FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/problems/src/backtracking/Permutations.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
/
Permutations.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (44 loc) · 1.24 KB
Breadcrumbs
leetcode
/
problems
/
src
/
backtracking
/
Permutations.java
Copy path
File metadata and controls
48 lines (44 loc) · 1.24 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
.
List
;
/**
* Created by gouthamvidyapradhan on 15/03/2017. Given a collection of distinct numbers, return all
* possible permutations.
*
* <p>For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1],
* [3,1,2], [3,2,1] ]
*/
public
class
Permutations
{
/**
* Main method
*
* @param args
* @throws Exception
*/
public
static
void
main
(
String
[]
args
)
throws
Exception
{
int
[]
nums
= {
1
,
2
,
3
};
List
<
List
<
Integer
>>
result
=
new
Permutations
().
permute
(
nums
);
}
public
List
<
List
<
Integer
>>
permute
(
int
[]
nums
) {
List
<
List
<
Integer
>>
result
=
new
ArrayList
<>();
nextPermutation
(
0
,
nums
,
result
);
return
result
;
}
private
void
nextPermutation
(
int
i
,
int
[]
nums
,
List
<
List
<
Integer
>>
result
) {
if
(
i
==
nums
.
length
-
1
) {
List
<
Integer
>
list
=
new
ArrayList
<>();
for
(
int
n
:
nums
)
list
.
add
(
n
);
result
.
add
(
list
);
}
else
{
for
(
int
j
=
i
,
l
=
nums
.
length
;
j
<
l
;
j
++) {
int
temp
=
nums
[
j
];
nums
[
j
] =
nums
[
i
];
nums
[
i
] =
temp
;
nextPermutation
(
i
+
1
,
nums
,
result
);
temp
=
nums
[
j
];
nums
[
j
] =
nums
[
i
];
nums
[
i
] =
temp
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL