本文主要是介绍C语言 | Leetcode C语言题解之第126题单词接龙II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
char** list;
int** back;
int* backSize;// DFS uses backtrack information to construct results
void dfs(char*** res, int* rSize, int** rCSizes, int* ans, int last, int retlevel) {int i = ans[last];if (i == 0) {res[*rSize] = (char**)malloc(sizeof(char*) * retlevel);(*rCSizes)[*rSize] = retlevel;for (int j = 0; j < retlevel; ++j) {res[*rSize][j] = list[ans[j]];}++(*rSize);}if (last == 0) return;for (int j = 0; j < backSize[i]; ++j) {int k = back[i][j];ans[last - 1] = k;dfs(res, rSize, rCSizes, ans, last - 1, retlevel);}
}char*** findLadders(char* beginWord, char* endWord, char** wordList, int wordListSize, int* returnSize, int** returnColumnSizes) {*returnSize = 0;int size = wordListSize + 1; // new sizeint wlen = strlen(beginWord);list = (char**)malloc(sizeof(char*) * size); // new wordlistback = (int**)malloc(sizeof(int*) * size); // data to backtrack indexes while BFSbackSize = (int*)malloc(sizeof(int) * size);int* visited = (int*)malloc(sizeof(int) * size); // visited flag in BFSint** diff = (int**)malloc(sizeof(int*) * size); // list diff[i] are indexes which can connect to word[i]int* diffSize = (int*)malloc(sizeof(int) * size);int endidx = 0;// Intialization 1for (int i = 0; i < size; ++i) {list[i] = i == 0 ? beginWord : wordList[i - 1];visited[i] = 0;diff[i] = (int*)malloc(sizeof(int) * size);diffSize[i] = 0;back[i] = (int*)malloc(sizeof(int) * size);backSize[i] = 0;if (strcmp(endWord, list[i]) == 0) {endidx = i;}}if (endidx == 0) return 0; // endword is not in the list// collect diff datafor (int i = 0; i < size; ++i) {for (int j = i; j < size; ++j) {int tmp = 0; // tmp is the difference between word[i] & word[j]for (int k = 0; k < wlen; ++k) {tmp += list[i][k] != list[j][k];if (tmp > 1) break;}if (tmp == 1) {diff[i][diffSize[i]++] = j;diff[j][diffSize[j]++] = i;}}}// BFSint* curr = (int*)malloc(sizeof(int) * size); // curr level in BFSint* prev = (int*)malloc(sizeof(int) * size); // prev level in BFSint prevSize, currSize = 1;int* currvisited = (int*)malloc(sizeof(int) * size); // to make sure curr members are uniqueint level = 1; // level of laddercurr[0] = 0;visited[0] = 1;int retlevel = 0; // ladder size, marks the end of BFSwhile (retlevel == 0 && currSize > 0) {++level;// switch prev and currint* tmp = prev;prev = curr;curr = tmp;prevSize = currSize;currSize = 0;// reset currvisited for each level currfor (int i = 0; i < size; ++i) {currvisited[i] = 0;}for (int i = 0; i < prevSize; ++i) {for (int j = 0; j < diffSize[prev[i]]; ++j) {int k = diff[prev[i]][j]; // k is the candidate of curr (next level of BFS)if (visited[k]) continue;back[k][backSize[k]++] = prev[i]; // backtrackif (k == endidx) retlevel = level; // find the lowest ladder level, finish current BFSif (currvisited[k]) continue; // no duplicates in curr listcurr[currSize++] = k;currvisited[k] = 1;}}// set visited after search through one level, different from Q127for (int i = 0; i < currSize; ++i) {visited[curr[i]] = 1;}}if (retlevel == 0) return 0; // no ladder found// DFS to construct resultchar*** res = (char***)malloc(sizeof(char**) * size);int* ans = (int*)malloc(sizeof(int) * retlevel);*returnColumnSizes = (int*)malloc(sizeof(int) * size);ans[retlevel - 1] = endidx;dfs(res, returnSize, returnColumnSizes, ans, retlevel - 1, retlevel);return res;
}
这篇关于C语言 | Leetcode C语言题解之第126题单词接龙II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!