LeetCode 127. Word Ladder(bfs)

题目来源:https://leetcode.com/problems/word-ladder/

127. Word Ladder

Medium

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

题意

类似LeetCode 126. Word Ladder II,不过只需要输出最短路长度

思路

典型的无向无权图的最短路问题,用bfs求解,主要数据结构是队列q和数组vis,队列q用于模拟bfs的过程,vis用于记录节点是否被访问过了

代码

class Solution {
public:
    class Node {
    public:
        int val, level; // value, length from beginWord
        
        Node(int _val, int _level): val(_val), level(_level) {}
        
//         bool operator < (const Node & other) const {
//             if (val < other.val) {
//                 return true;
//             } else if (val > other.val) {
//                 return false;
//             } else {
//                 return level < other.level;
//             }
//         }
    };   
    
    // check if $a and $b differs on only one letter
    static bool differCnt(const string &a, const string &b) {
        int cnt = 0, i = 0, n = a.length();
        for (i=0; i 1) {
                    return false;
                }
            }
        }
        return cnt == 1;
    }
    
    // find index of endWord in wordList
    // return -1 if not found
    static int findEnd(string target, const vector &wordList) {
        int i = 0, n = wordList.size();
        for (i=0; i& wordList) {
        int n = wordList.size();
        if (n == 0) {           // empty $wordList
            return 0;
        }
        int eind = findEnd(endWord, wordList);  // index of $endWord in $wordList
        if (eind == -1) {   // $endWord not found in $wordList
            return 0;
        }
        // construct graph
        // nodes: $n words from $wordList and $beginWord as $n+1
        // edges: if node $i and $j differs on only one letter, than there exist edge $i=>$j and edge $j=>$i
        wordList.push_back(beginWord);
        vector > graph(n+1, vector()); // adjacent list
        int i = 0, j = 0;
        for (i=0; i q;                      // bfs queue
        vector vis(n+1);              // if node is visited by bfs
        q.push(Node(n, 1));                 // from beginWord
        vis[n] = true;
        while (!q.empty()) {
            Node head = q.front();
            q.pop();
            for (auto item: graph[head.val]) {
                if (!vis[item]) {
                    if (item == eind) {
                        return head.level+1;    // path found
                    }
                    vis[item] = true;
                    q.push(Node(item, head.level+1));
                }
            }
        }
        return 0;           // path not found
    }
};

 

你可能感兴趣的:(LeetCode)