1. 程式人生 > 實用技巧 >leetcode日題 127. 單詞接龍

leetcode日題 127. 單詞接龍

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        //優化,雙向BFS
        //優先遍歷兩個佇列中size較小的
        queue<string> open1;
        queue<string> open2;
        unordered_set<string> close1;
        unordered_set<string
> close2; unordered_set<string> worddit(wordList.begin(),wordList.end()); //這裡的集合用於更改狀態 int depth1=0; if(!worddit.count(endWord)){ return 0; } //雙向BFS需要判斷起點是否為終點 int test=0; for(int i=0;i<beginWord.size();i++){
if(beginWord[i]!=endWord[i]){ test++; } } if(test==1){ return 2; } open1.push(beginWord); open2.push(endWord); string cur; while(!open1.empty()&&!open2.empty()){ if(open1.size()>open2.size()){ queue
<string> temp1=open1; open1=open2; open2=temp1; unordered_set<string> temp2=close1; close1=close2; close2=temp2; } int n1=open1.size(); //這裡必須要逐層遍歷,不然深度的增加會出錯 while(n1--){ cur=open1.front(); open1.pop(); //改用 逐個字母修改 if(close2.count(cur)){ return depth1; } if(close1.count(cur)){ continue; } close1.insert(cur); for(auto& cbyte:cur){ for(char i='a';i<='z';i++){ char temp=cbyte; cbyte=i; if(worddit.count(cur)&&temp!=i){ open1.push(cur); } cbyte=temp; } } } depth1++; } return 0; // //雙向BFS // queue<string> open1; // queue<string> open2; // unordered_set<string> close1; // unordered_set<string> close2; // unordered_set<string> worddit(wordList.begin(),wordList.end()); // //也可以用close表,這裡用集合代替,訪問過的單詞會被擦除 // int depth1=0; // int depth2=0; // if(!worddit.count(endWord)){ // return 0; // } // //雙向BFS需要判斷起點是否為終點 // int test=0; // for(int i=0;i<beginWord.size();i++){ // if(beginWord[i]!=endWord[i]){ // test++; // } // } // if(test==1){ // return 2; // } // open1.push(beginWord); // open2.push(endWord); // string cur; // while(!open1.empty()&&!open2.empty()){ // int n1=open1.size(); // //這裡必須要逐層遍歷,不然深度的增加會出錯 // while(n1--){ // cur=open1.front(); // open1.pop(); // //改用 逐個字母修改 // if(close2.count(cur)){ // return depth1+depth2; // } // if(close1.count(cur)){ // continue; // } // close1.insert(cur); // for(auto& cbyte:cur){ // for(char i='a';i<='z';i++){ // char temp=cbyte; // cbyte=i; // if(worddit.count(cur)&&temp!=i){ // if(close1.count(cur)){ // //排除無效的狀態 // //這裡不排除會出錯 // cbyte=temp; // continue; // } // open1.push(cur); // } // cbyte=temp; // } // } // } // depth1++; // int n2=open2.size(); // while(n2--){ // cur=open2.front(); // open2.pop(); // if(close1.count(cur)){ // return depth1+depth2; // } // if(close2.count(cur)){ // continue; // } // close2.insert(cur); // //改用 逐個字母修改 // for(auto& cbyte:cur){ // for(char i='a';i<='z';i++){ // char temp=cbyte; // cbyte=i; // if(worddit.count(cur)&&temp!=i){ // if(close2.count(cur)){ // //排除無效的狀態 // //這裡不排除會出錯 // cbyte=temp; // continue; // } // open2.push(cur); // } // cbyte=temp; // } // } // } // depth2++; // } // return 0; // bfs // queue<string> open; // unordered_set<string> worddit(wordList.begin(),wordList.end()); // //也可以用close表,這裡用集合代替,訪問過的單詞會被擦除 // int ans=1; // if(!worddit.count(endWord)){ // return 0; // } // open.push(beginWord); // string cur; // while(!open.empty()){ // int n=open.size(); // //這裡必須要逐層遍歷,不然深度的增加會出錯 // while(n--){ // cur=open.front(); // open.pop(); // if(cur==endWord){ // return ans; // } // //此方法超時 // // for(auto word:wordList){ // // int p=0; // // for(int i=0;i<word.size();i++){ // // if(word[i]!=cur[i]){ // // p++; // // } // // } // // if(p==1&&worddit.count(word)){ // // open.push(word); // // worddit.erase(word); // // } // // } // //改用 逐個字母修改 // for(auto& cbyte:cur){ // for(char i='a';i<='z';i++){ // char temp=cbyte; // cbyte=i; // if(worddit.count(cur)&&temp!=i){ // open.push(cur); // worddit.erase(cur); // } // cbyte=temp; // } // } // } // ans++; // } // return 0; } };