牛客网leetcode刷题笔记

7/6:

    做的查找中的word_ladder,没做出来。

        学到了的知识点:

            unordered_set:

                定义为unordered_set<string>dict

                在集合中查找为dict.find(cur_front),找没找到用if (dict.find(cur_front) != dict.end())

            queue:

                定义为queue<string>Q

                插入为Q.push(start)

                弹出为Q.pop()

                判断是否为空为Q.empty()

扫描二维码关注公众号,回复: 1919960 查看本文章

                查看长度为Q.size()

                取值为Q.front()

            在一个单词中替换单个字母为:

            for (int i = 0; i < size; i++){
                   char ch = cur_front[i];  //对队列头字符串每一个字符进行替换
                   for (int j = 0; j < 26; j++){
                       cur_front[i] = 'a' + j;//替换字符
                       if (cur_front == end)return res+1;//找到答案,退出
                       if (dict.find(cur_front) != dict.end()){
                           Q.push(cur_front);//变换在字典中找到了
                           dict.erase(cur_front);//从字典中删除
                       }
                   }
                   cur_front[i] = ch;//还原队列头字符串,因为有可能在字典中可以找到多个“近邻字符串”
               }
            

                

猜你喜欢

转载自blog.csdn.net/weixin_38104825/article/details/80946471