花花的讲解很棒。只是我对单广的时间复杂度有疑问,你的答案是不是太loose了。 BFS 时间复杂度一般的就是O(V+E), here V is len(wordlist), each word's neighbor is o(len(word) * 26). so E should be o(V*26*len(word)),so BFS is just O(V+V*26*len(word)) which is O(len(wordlist)*len(word)). Plus the time for build path which is O(number of solutions). which can be ignored compared to BFS time cost. so total time complexity is O(len(wordlist)*len(word)). Plz correct me if I am wrong. Many thanks
在找到endWord的那个if branch结束处可以直接continue. 因为在一开始已经做过dict.erase(endWord). if (endWord==w) { parents[w].push_back(p); found = true; continue; // endWord is already erased at the beginning. } 所以两句parents[w].push_back(p) 不会在同一个iteration里同时被hit