URL
链接:https://leetcode.cn/problems/di-string-match/
题目
分析
源码
工程结构
.
└── main_942.c
源码文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int * di_string_match(char * s, int * return_size){
int n = strlen(s);
int lo = 0, hi = n;
int *perm = (int *)malloc(sizeof(int) * (n+1));
for (int i = 0; i < n; ++i){
perm[i] = (s[i] == 'I' ? lo++ : hi--);
}
perm[n] = lo;
*return_size = n + 1;
return perm;
}
int main()
{
char data[][8]={
"IDID",
"III",
"DDI"
};
int size = sizeof(data)/sizeof(data[0]);
for(int i = 0; i < size; ++i){
int ret_size = -1;
int * p = NULL;
p = di_string_match(data[i],&ret_size);
printf("data[%d] = \"%s\", perm = [ ",i,data[i]);
for (int j = 0; j < ret_size; j++){
printf("%d ", p[j]);
}
printf("]\n");
if(p != NULL){
free(p);
}
}
return 0;
}
LOG参考
data[0] = "IDID", perm = [ 0 4 1 3 2 ]
data[1] = "III", perm = [ 0 1 2 3 ]
data[2] = "DDI", perm = [ 3 2 0 1 ]
源码概述
略
小结
略
|