leetcode 127 单词接龙

简介: leetcode 127 单词接龙

单词接龙


8db8a4ca90fe47f3a0bd72bb4c2c722f.png

深度搜索(超时)

class Solution {
public:
    int result = INT_MAX;
    int tmp_result = 1;
    int cheak(string s1 , string s2)
    {
        int no_same_num = 0;
        for(int i=0 ; i<s1.size() ;i++)
        {
            if(s1[i] != s2[i]) no_same_num++;
        }
        return no_same_num;
    }
    void track_back(string beginWord, string endWord, vector<string>& wordList , int indix, vector<bool> &path)
    {
        for(int i=0 ; i<wordList.size() ;i++)
        {
           if(wordList[indix] == endWord && path[i] == false)
            {
                if(tmp_result < result) result = tmp_result;
                // cout<<endl;
                return;
            }else if(cheak(wordList[indix],wordList[i]) == 1 && path[i] == false)
            {
                // cout<<wordList[i]<<' ';
                tmp_result ++;
                path[i] = true;
                track_back(beginWord,endWord,wordList,i,path);
                path[i] = false;
                tmp_result--;
            }
        }
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        vector<bool> path(wordList.size(),false);
        for(int i=0 ; i<wordList.size() ;i++)
        {
            if(cheak(beginWord,wordList[i]) == 1 && path[i]==false)
            {
                // cout<<wordList[i]<<' ';
                path[i] = true;
                tmp_result++;
                track_back(beginWord,endWord,wordList,i,path);
                tmp_result--;
                path[i] = false;
            }
        }
        if(result == INT_MAX) return 0;
        return result;
    }
};

广度搜索

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        // 将vector转成unordered_set,提高查询速度
        unordered_set<string> wordSet(wordList.begin(), wordList.end());
        // 如果endWord没有在wordSet出现,直接返回0
        if (wordSet.find(endWord) == wordSet.end()) return 0;
        // 记录word是否访问过
        unordered_map<string, int> visitMap; // <word, 查询到这个word路径长度>
        // 初始化队列
        queue<string> que;
        que.push(beginWord);
        // 初始化visitMap
        visitMap.insert(pair<string, int>(beginWord, 1));
        while(!que.empty()) 
        {
            string word = que.front();
            que.pop();
            int path = visitMap[word]; // 这个word的路径长度
            for (int i = 0; i < word.size(); i++)
            {
                string newWord = word; // 用一个新单词替换word,因为每次置换一个字母
                for (int j = 0 ; j < 26; j++) 
                {
                    newWord[i] = j + 'a';
                    if (newWord == endWord) return path + 1; // 找到了end,返回path+1
                    // wordSet出现了newWord,并且newWord没有被访问过
                    if (wordSet.find(newWord) != wordSet.end() && visitMap.find(newWord) == visitMap.end()) 
                    {
                        // 添加访问信息
                        visitMap.insert(pair<string, int>(newWord, path + 1));
                        que.push(newWord);
                    }
                }
            }
        }
        return 0;
    }
};
相关文章
|
6天前
leetcode-127:单词接龙
leetcode-127:单词接龙
21 0
|
6天前
leetcode127单词接龙刷题打卡
leetcode127单词接龙刷题打卡
17 0
|
10月前
|
算法 Java C++
leetcode单词接龙
leetcode单词接龙
☆打卡算法☆LeetCode 127. 单词接龙 算法解析
“给定两个单词beginWord和endWord,以及一个字典wordList,找出并返回所有从beginWord到endWrod之间的最短转换序列中的单词数目。”
☆打卡算法☆LeetCode 126. 单词接龙 II 算法解析
“给定两个单词beginWord和endWord,以及一个字典wordList,找出并返回所有从beginWord到endWrod之间的最短转换序列。”
|
算法
​LeetCode刷题实战126:单词接龙 II
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
195 0
[leetcode/lintcode 题解] 阿里算法面试题:单词接龙 II
[leetcode/lintcode 题解] 阿里算法面试题:单词接龙 II
[leetcode/lintcode 题解] 阿里算法面试题:单词接龙 II
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
6天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
13 0