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;
    }
};
相关推荐
崎岖Qiu18 小时前
leetcode380:RandomizedSet - O(1)时间插入删除和获取随机元素(数组+哈希表的巧妙结合)
java·数据结构·算法·leetcode·力扣·散列表
好易学·数据结构19 小时前
可视化图解算法60: 矩阵最长递增路径
数据结构·算法·leetcode·力扣·递归·回溯算法·牛客
大锦终19 小时前
【算法】栈专题
数据结构·c++·算法·leetcode
天选之女wow19 小时前
【代码随想录算法训练营——Day6(Day5周日休息)】哈希表——242.有效的字母异位词、349.两个数组的交集、202.快乐数、1.两数之和
数据结构·算法·leetcode·散列表
我想吃烤肉肉20 小时前
leetcode-python-2154将找到的值乘以 2
python·算法·leetcode
Mr_Xuhhh1 天前
项目需求分析(2)
c++·算法·leetcode·log4j
Morri31 天前
[Java恶补day53] 45. 跳跃游戏Ⅱ
java·算法·leetcode
林木辛1 天前
LeetCode热题 15.三数之和(双指针)
算法·leetcode·双指针
和光同尘@1 天前
66. 加一 (编程基础0到1)(Leetcode)
数据结构·人工智能·算法·leetcode·职场和发展
CHEN5_021 天前
leetcode-hot100 11.盛水最多容器
java·算法·leetcode