Home > Back-end >  Why ask bosses, leetcode the permutation and combination problem of output
Why ask bosses, leetcode the permutation and combination problem of output

Time:10-28


Enter a string, arrange to print out all of the characters in this string,



You can return the string array in any order, but it can't have a repeating element,



Example:

Input: s="ABC" output: [" ABC ", "acb", "America", "bca", "cab", "cba"]


Title url: https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/solution/


I run error code

The class Solution (object) :
LST=[]
Def permutation (self, s) :
"" "
: type s: STR
Rtype: List (STR)
"" "
Self. Perm (list (s), and 0, len (s))
Return a list (sorted (set (self. LST)))
Def perm (self, s, k, m) :
If k==m:
The self. The LST. Append (". "the join (s))
The else:
For I in range (k, m) :
S [I], [k] s=s [k], [I] s
Self. Perm (s, k + 1, m)
S [I], [k] s=s [k], [I] s

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
# include & lt; String. H>
# include & lt; Stdlib. H>
int m;//record the length of the string
int n;//record the number of types of characters in a string
Char map [256].//record is what kind of character
Int count [256];//record each character how many
Int stack [1000];//recursive stack, and record the arrangement of the current generation
Void Make_Map (char * STR) {//statistics the related information of the string
Int s [256];
int i;
Memset (s, 0, sizeof (s));
Memset (count, 0, sizeof (count));
M=strlen (STR);
While (* STR) {
S [* STR] + +;
Str++;
}
N=0;
For (I=0; i<256; I++)
If (s) [I] {
The map [n]=I;
The count [n] [I]=s;
n++;
}
}
Void the Find (int the depth) {//recursion backtracking generating permutations
If (the depth==m) {
int i;
For (I=0; iputchar('\n');
} else {
int i;
For (I=0; iIf (count [I]) {
Stack [the depth]=I;
The count [I] -;
Find the depth (+ 1);
The count [I] + +;
}
}
}
Void main (int arg c, char * * argv) {
If (argc<2) {
Printf (" % s to produce the whole arrangement of string \ n ", argv [0]).
return;
}
Make_Map (argv [1]);
Find (0);
}
  • Related