当前位置:网站首页>Interview question 08.07. Permutation and combination of non repeated strings DFS method
Interview question 08.07. Permutation and combination of non repeated strings DFS method
2022-07-25 03:36:00 【Mr Gao】
Interview questions 08.07. Permutations of non repeating strings
Permutations of non repeating strings . Write a method , Calculates all permutations of a string , Each character of the string is different .
Example 1:
Input :S = “qwe”
Output :[“qwe”, “qew”, “wqe”, “weq”, “ewq”, “eqw”]
Example 2:
Input :S = “ab”
Output :[“ab”, “ba”]
The code solver is as follows , Maybe the code looks complex , Just practice more
/** * Note: The returned array must be malloced, assume caller calls free(). */
int size;
void dfs(int nowlen,int len,char **re,char *s,char *t,int *r){
int i=0;
if(nowlen==len){
re[size]=(char *)malloc(sizeof(char)*(len+1));
for(i=0;i<len;i++){
re[size][i]=t[i];
}
re[size][i]='\0';
size++;
}
else{
for(i=0;i<len;i++){
if(r[i]==1){
t[nowlen]=s[i];
r[i]=0;
dfs(nowlen+1,len,re,s,t,r);
r[i]=1;
}
}
}
}
int f(int n){
int re=1;
while(n>=1){
re=re*n;
n--;
}
return re;
}
char** permutation(char* S, int* returnSize){
int len=strlen(S);
char **re=(char **)malloc(sizeof(char *)*f(len));
char* t=(char *)malloc(sizeof(char)*len);
int *r=(int *)malloc(sizeof(int)*len);
int i=0;
for(i=0;i<len;i++){
r[i]=1;
}
size=0;
for(i=0;i<len;i++){
if(r[i]==1){
t[0]=S[i];
r[i]=0;
dfs(1,len,re,S,t,r);
r[i]=1;
}
}
* returnSize=size;
return re;
}
边栏推荐
- Skywalking distributed link tracking, related graphics, DLJD, cat
- Select sort / cardinality sort
- [Flink] transform operator filter
- .net6 miniapi (V): Options
- Lombok detailed introduction
- [golang] golang realizes sending wechat service number template messages
- "Introduction to interface testing" punch in to learn day04: how to abstract the procedural test script into a test framework?
- C language_ Structure introduction
- How to use two stacks to simulate the implementation of a queue
- NVM installation and use
猜你喜欢

Question D: pruning shrubs

Image processing based on hog feature

Detailed explanation of three factory modes

Analysis of browser working principle

C language_ Defining structures and using variables

Hal library serial port for note taking

Use reflection to convert RDD to dataframe

Flink1.15 source code reading - Flink annotations

Advantages and disadvantages of zero trust security

It took me 2 years from Foxconn assembly line to Tencent software testing post~
随机推荐
Openlayers draw deletes the last point when drawing
B. Making Towers
Swiper4 is used to smooth longitudinal seamless scrolling. After clicking or dragging the mouse, the animation is not completely completed, the mouse moves out of the automatic rotation, and the dynam
基于ABP实现DDD--领域逻辑和应用逻辑
Execution flow control of shell
[Flink] transform operator filter
MySQL select query part 2
VMware installation
Vscode copy synchronization plug-in expansion
What should testers do if they encounter a bug that is difficult to reproduce?
The relationship between private domain traffic and fission marketing. What is super app? Can our enterprise own it?
Fiddler grabs packets and displays err_ TUNNEL_ CONNECTION_ FAILED
Moveit2 - 7. Scenario planning ROS API
Take a note: Oracle conditional statement
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
Analysis of browser working principle
Codewars notes
10. 509 Certificate (structure + principle)
Using one stack to sort another stack
Question B: shunzi date