• leetcode做题笔记126. 单词接龙 II


    按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> s1 -> s2 -> ... -> sk 这样的单词序列,并满足:

    • 每对相邻的单词之间仅有单个字母不同。
    • 转换过程中的每个单词 si1 <= i <= k)必须是字典 wordList 中的单词。注意,beginWord 不必是字典 wordList 中的单词。
    • sk == endWord

    给你两个单词 beginWord 和 endWord ,以及一个字典 wordList 。请你找出并返回所有从 beginWord 到 endWord 的 最短转换序列 ,如果不存在这样的转换序列,返回一个空列表。每个序列都应该以单词列表 [beginWord, s1, s2, ..., sk] 的形式返回。

    思路一:BFS

    1. char** list;
    2. int** back;
    3. int* backSize;
    4. void dfs(char*** res, int* rSize, int** rCsize, int* ans, int last, int retLevel){
    5. int i = ans[last];
    6. if(i == 0){
    7. res[*rSize] = (char**)malloc(sizeof(char*) * retLevel);
    8. (*rCsize)[*rSize] = retLevel;
    9. for(int j = 0; j < retLevel; j++){
    10. res[*rSize][j] = list[ans[j]];
    11. }
    12. (*rSize)++;
    13. }
    14. if(last == 0){
    15. return;
    16. }
    17. for(int j = 0; j < backSize[i]; j++){
    18. int k = back[i][j];
    19. ans[last-1] = k;
    20. dfs(res,rSize,rCsize,ans,last-1,retLevel);
    21. }
    22. }
    23. char *** findLadders(char * beginWord, char * endWord, char ** wordList, int wordListSize, int* returnSize, int** returnColumnSizes){
    24. *returnSize = 0;
    25. int size = wordListSize+1;
    26. int wlen = strlen(beginWord);
    27. list = (char**)malloc(sizeof(char*)*size); back = (int**)malloc(sizeof(int*) * size);
    28. backSize = (int*)malloc(sizeof(int) * size);
    29. int* visited = (int*)malloc(sizeof(int) * size);
    30. int** diff = (int**)malloc(sizeof(int*) * size);
    31. int* diffSize = (int*)malloc(sizeof(int) * size);
    32. int endidx = 0;
    33. for (int i = 0; i < size; ++i) {
    34. list[i] = i == 0 ? beginWord : wordList[i - 1];
    35. visited[i] = 0;
    36. diff[i] = (int*)malloc(sizeof(int) * size);
    37. diffSize[i] = 0;
    38. back[i] = (int*)malloc(sizeof(int) * size);
    39. backSize[i] = 0;
    40. if (strcmp(endWord, list[i]) == 0) {
    41. endidx = i;
    42. }
    43. }
    44. if (endidx == 0) return 0; // endword is not in the list
    45. // collect diff data
    46. for (int i = 0; i < size; ++i) {
    47. for (int j = i; j < size; ++j) {
    48. int tmp = 0; // tmp is the difference between word[i] & word[j]
    49. for (int k = 0; k < wlen; ++k) {
    50. tmp += list[i][k] != list[j][k];
    51. if (tmp > 1) break;
    52. }
    53. if (tmp == 1) {
    54. diff[i][diffSize[i]++] = j;
    55. diff[j][diffSize[j]++] = i;
    56. }
    57. }
    58. }
    59. // BFS
    60. int* curr = (int*)malloc(sizeof(int) * size);
    61. int* prev = (int*)malloc(sizeof(int) * size);
    62. int prevSize, currSize = 1;
    63. int* currvisited = (int*)malloc(sizeof(int) * size);
    64. int level = 1;
    65. curr[0] = 0;
    66. visited[0] = 1;
    67. int retlevel = 0;
    68. while (retlevel == 0 && currSize > 0) {
    69. ++level;
    70. int* tmp = prev;
    71. prev = curr;
    72. curr = tmp;
    73. prevSize = currSize;
    74. currSize = 0;
    75. for (int i = 0; i < size; ++i) {
    76. currvisited[i] = 0;
    77. }
    78. for (int i = 0; i < prevSize; ++i) {
    79. for (int j = 0; j < diffSize[prev[i]]; ++j) {
    80. int k = diff[prev[i]][j];
    81. if (visited[k]) continue;
    82. back[k][backSize[k]++] = prev[i];
    83. if (k == endidx) retlevel = level;
    84. if (currvisited[k]) continue;
    85. curr[currSize++] = k;
    86. currvisited[k] = 1;
    87. }
    88. }
    89. for (int i = 0; i < currSize; ++i) {
    90. visited[curr[i]] = 1;
    91. }
    92. }
    93. if (retlevel == 0) return 0;
    94. char*** res = (char***)malloc(sizeof(char**) * size);
    95. int* ans = (int*)malloc(sizeof(int) * retlevel);
    96. *returnColumnSizes = (int*)malloc(sizeof(int) * size);
    97. ans[retlevel - 1] = endidx;
    98. dfs(res, returnSize, returnColumnSizes, ans, retlevel - 1, retlevel);
    99. return res;
    100. }

    分析:

    本题采用广度优先搜索将每个字符串能转换的所有序列找出,再判断是否存在最短转换序列,最后输出答案

    总结:

    本题考察广度优先搜索的应用,判断当前字符是否匹配,得到转换序列即可做出

  • 相关阅读:
    【Qt+FFMPEG】- FFMPEG转码详细流程
    数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
    PyTorch中实现Transformer模型
    echarts——实现大屏数据展示——基础积累(超详细)
    YOLO-World:实时开放词汇目标检测
    Oracle数据库的逻辑结构
    猿创征文|UDP/TCP网络编程
    Mybatis之动态SQL
    Docker 的数据管理
    redis的配置文件
  • 原文地址:https://blog.csdn.net/si_mple_/article/details/132678195