# C语言 | Leetcode C语言题解之第332题重新安排行程

371 篇文章 2 订阅

char* id2str[26 * 26 * 26];

int str2id(char* a) {
int ret = 0;
for (int i = 0; i < 3; i++) {
ret = ret * 26 + a[i] - 'A';
}
return ret;
}

int cmp(const void* _a, const void* _b) {
int **a = (int**)_a, **b = (int**)_b;
return (*b)[0] - (*a)[0] ? (*b)[0] - (*a)[0] : (*b)[1] - (*a)[1];
}

int* vec[26 * 26 * 26];
int vec_len[26 * 26 * 26];
int* stk;
int stk_len;

void dfs(int curr) {
while (vec_len[curr] > 0) {
int tmp = vec[curr][--vec_len[curr]];
dfs(tmp);
}
stk[stk_len++] = curr;
}

char** findItinerary(char*** tickets, int ticketsSize, int* ticketsColSize, int* returnSize) {
memset(vec_len, 0, sizeof(vec_len));
stk = malloc(sizeof(int) * (ticketsSize + 1));
stk_len = 0;

int* tickets_tmp[ticketsSize];
for (int i = 0; i < ticketsSize; i++) {
tickets_tmp[i] = (int*)malloc(sizeof(int) * 2);
tickets_tmp[i][0] = str2id(tickets[i][0]);
tickets_tmp[i][1] = str2id(tickets[i][1]);
id2str[tickets_tmp[i][0]] = tickets[i][0];
id2str[tickets_tmp[i][1]] = tickets[i][1];
}
qsort(tickets_tmp, ticketsSize, sizeof(int*), cmp);

}
vec[start] = malloc(sizeof(int) * vec_len[start]);
}
}

dfs(str2id("JFK"));

*returnSize = ticketsSize + 1;
char** ret = malloc(sizeof(char*) * (ticketsSize + 1));
for (int i = 0; i <= ticketsSize; i++) {
ret[ticketsSize - i] = id2str[stk[i]];
}
return ret;
}
• 8
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
04-30 625
04-25 536
05-06 492

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。