FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/PuzzleCoding/src/CombinationPermutation.java at master · wodgithub/algorithm · GitHub
wodgithub
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
PuzzleCoding
/
src
/
CombinationPermutation.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
71 lines (62 loc) · 2.05 KB
Breadcrumbs
algorithm
/
PuzzleCoding
/
src
/
CombinationPermutation.java
Copy path
File metadata and controls
71 lines (62 loc) · 2.05 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import
java
.
util
.
ArrayList
;
public
class
CombinationPermutation
{
public
static
void
main
(
String
[]
args
) {
String
s
=
"abcd"
;
int
k
=
3
;
System
.
out
.
println
(
"Combination"
);
combinationK
(
""
,
s
,
k
);
System
.
out
.
println
(
"Permutation"
);
permutationK
(
s
.
toCharArray
(),
s
.
length
(),
k
);
System
.
out
.
println
(
"Permutation1"
);
permutationK1
(
""
,
s
,
k
);
System
.
out
.
println
(
"MoveInOut"
);
move
(
s
.
toCharArray
(),
s
.
length
(),
new
ArrayList
<
String
>(),
true
);
}
public
static
void
combinationK
(
String
prefix
,
String
s
,
int
K
) {
if
(
K
==
0
) {
System
.
out
.
println
(
prefix
);
return
;
}
for
(
int
i
=
0
;
i
<
s
.
length
(); ++
i
)
combinationK
(
prefix
+
s
.
charAt
(
i
),
s
.
substring
(
i
+
1
),
K
-
1
);
}
public
static
void
permutationK1
(
String
prefix
,
String
s
,
int
K
) {
if
(
K
==
0
) {
System
.
out
.
println
(
prefix
);
return
;
}
for
(
int
i
=
0
;
i
<
s
.
length
(); ++
i
)
permutationK1
(
prefix
+
s
.
charAt
(
i
),
s
.
substring
(
0
,
i
)+
s
.
substring
(
i
+
1
),
K
-
1
);
}
public
static
void
permutationK
(
char
[]
s
,
int
n
,
int
K
) {
if
(
K
==
0
) {
for
(
int
i
=
n
;
i
<
s
.
length
; ++
i
)
System
.
out
.
print
(
s
[
i
]);
System
.
out
.
println
();
return
;
}
for
(
int
i
=
0
;
i
<
n
; ++
i
) {
swap
(
s
,
i
,
n
-
1
);
permutationK
(
s
,
n
-
1
,
K
-
1
);
swap
(
s
,
i
,
n
-
1
);
}
}
public
static
void
swap
(
char
[]
a
,
int
left
,
int
right
) {
char
tmp
=
a
[
left
];
a
[
left
] =
a
[
right
];
a
[
right
] =
tmp
;
}
public
static
void
move
(
char
[]
s
,
int
n
,
ArrayList
<
String
>
result
,
boolean
pick
) {
if
(
n
==
0
) {
System
.
out
.
println
(
result
.
toString
());
return
;
}
move
(
s
,
n
-
1
,
result
,
true
);
if
(
pick
) {
result
.
add
(
String
.
valueOf
(
s
[
n
-
1
]));
}
else
{
result
.
remove
(
String
.
valueOf
(
s
[
n
-
1
]));
}
move
(
s
,
n
-
1
,
result
,
false
);
}
}
Back
|
FazBrowse Home
|
New Git URL