leetcode 127. 单词接龙

题目:127. 单词接龙 - 力扣(LeetCode)

先建立一颗trie树,从beginWord开始bfs;bfs的过程中,对trie树进行dfs寻找"只差一个字母"的其他未遍历到的字符串;直到bfs遍历到endWord。

cpp 复制代码
struct Node {
    Node** c;
    string str;
    bool v = false;
    Node() {
        c = (Node**) malloc(26 * sizeof(Node*));
        memset(c, 0, 26 * sizeof(Node*));
    }
};
class Solution {
public:
    void dfs(list<string>& bfs, list<int>& val, const string& s, int path, Node* node, int idx, bool changed) {
        char c = s[idx] - 'a';
        Node* t;
        if (idx == s.length() - 1) {
            if (changed) {
                t = node->c[c];
                if (t && !t->v) {
                    t->v = true;
                    bfs.push_back(t->str);
                    val.push_back(path + 1);
                }
            } else {
                for (int i = 0; i < 26; i++) {
                    if (i == c) continue;
                    t = node->c[i];
                    if (t && !t->v) {
                        t->v = true;
                        bfs.push_back(t->str);
                        val.push_back(path + 1);
                    }
                }
            }
            return;
        }
        
        for (int i = 0; i < 26; i++) {
            if (!node->c[i]) {
                continue;
            }
            if (i == c) {
                dfs(bfs, val, s, path, node->c[i], idx + 1, changed);
            } else if (!changed) {
                dfs(bfs, val, s, path, node->c[i], idx + 1, true);
            }
        }
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        Node* root = new Node;
        bool hasEndWord = false;
        char c;
        Node* t;
        string s;
        for (int i = 0; i < wordList.size(); i++) {
            s = wordList[i];
            if (s.compare(endWord) == 0) {
                hasEndWord = true;
            }
            t = root;
            for (int j = 0; j < s.length(); j++) {
                c = s[j] - 'a';
                if (!t->c[c]) {
                    t->c[c] = new Node;
                }
                t = t->c[c];
            }
            t->str = s;
        }
        if (!hasEndWord) {
            return 0;
        }
        
        list<string> bfs;
        bfs.push_back(beginWord);
        list<int> val;
        val.push_back(1);
        int path;
        while (!bfs.empty()) {
            s = bfs.front();
            bfs.pop_front();
            path = val.front();
            val.pop_front();
//            printf("** %s %d\n", s.data(), path);
            if (s.compare(endWord) == 0) {
                return path;
            }
            dfs(bfs, val, s, path, root, 0, false);
        }
        
        return 0;
    }
};
相关推荐
木子墨51614 小时前
LeetCode 热题 100 精讲 | 计算几何篇:点积叉积 · 线段相交 · 凸包 · 多边形面积
c++·算法·leetcode·职场和发展·动态规划
py有趣14 小时前
力扣热门100题之最小路径和
算法·leetcode
im_AMBER15 小时前
Leetcode 159 无重复字符的最长子串 | 长度最小的子数组
javascript·数据结构·学习·算法·leetcode
郝学胜-神的一滴15 小时前
[力扣 105]二叉树前中后序遍历精讲:原理、实现与二叉树还原
数据结构·c++·算法·leetcode·职场和发展
sheeta199815 小时前
LeetCode 每日一题笔记 日期:2026.04.20 题目:2078.两栋颜色不同而距离最远的房子
笔记·算法·leetcode
承渊政道16 小时前
【递归、搜索与回溯算法】(floodfill算法:从不会做矩阵题,到真正掌握搜索扩散思想)
数据结构·c++·算法·leetcode·矩阵·dfs·bfs
剑挑星河月16 小时前
73.矩阵置零
数据结构·算法·leetcode·矩阵
_深海凉_16 小时前
LeetCode热题100-单词拆分
算法·leetcode·职场和发展
_深海凉_1 天前
LeetCode热题100-有效的括号
linux·算法·leetcode
@BangBang1 天前
leetcode (4): 连通域/岛屿问题
算法·leetcode·深度优先