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

返回它的长度 5。

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

BFS 广度优先搜索 中档题 但是 我不会

``````class Solution {
public:
unordered_map<string, int> wordId;
vector<vector<int>> edge;
int nodeNum = 0;

if (!wordId.count(word)) {
wordId[word] = nodeNum++;
edge.emplace_back();
}
}

int id1 = wordId[word];
for (char& it : word) {
char tmp = it;
it = '*';
int id2 = wordId[word];
edge[id1].push_back(id2);
edge[id2].push_back(id1);
it = tmp;
}
}

int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
for (string& word : wordList) {
}
if (!wordId.count(endWord)) {
return 0;
}
vector<int> dis(nodeNum, INT_MAX);
int beginId = wordId[beginWord], endId = wordId[endWord];
dis[beginId] = 0;

queue<int> que;
que.push(beginId);
while (!que.empty()) {
int x = que.front();
que.pop();
if (x == endId) {
return dis[endId] / 2 + 1;
}
for (int& it : edge[x]) {
if (dis[it] == INT_MAX) {
dis[it] = dis[x] + 1;
que.push(it);
}
}
}
return 0;
}
};
``````

class Solution {
public:
unordered_map<string, int> wordId;
vector<vector<int>> edge;
int nodeNum = 0;

### 使用了虚拟节点，如 hit 不但 插入 hit 到图里，还把 hi* ，h*t， *it 放进图里 所以距离是两倍 最后  return dis[endId] / 2 + 1;

if (!wordId.count(word)) {
wordId[word] = nodeNum++;
edge.emplace_back();
}
}

### 同时 edge.emplace_back(); 是 往 edge 加入一个 空 vector  就是 edge[nodeNum-1] 后面把边 可以插进去

int id1 = wordId[word];
for (char& it : word) {
char tmp = it;
it = '*';
int id2 = wordId[word];
edge[id1].push_back(id2);
edge[id2].push_back(id1);
it = tmp;
}
}

for (char& it : word) {
char tmp = it;
it = '*';

。。。

it = tmp;

}

### 把替代的 * 的虚拟节点 加入   用 it 遍历，后面 it = tmp 恢复 word 原样

int id2 = wordId[word];
edge[id1].push_back(id2);
edge[id2].push_back(id1);

### 获取节点 和虚节点 的下标，存入边内

int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
for (string& word : wordList) {
}

### 存边 存点

if (!wordId.count(endWord)) {
return 0;
}

### 判断 目的 字符串 是否存在

vector<int> dis(nodeNum, INT_MAX);
int beginId = wordId[beginWord], endId = wordId[endWord];

### string 替换成 id 去匹配

dis[beginId] = 0;

queue<int> que;
que.push(beginId);

### 用 队列实现 广度优先搜索

while (!que.empty()) {
int x = que.front();
que.pop();
if (x == endId) {
return dis[endId] / 2 + 1;
}

### 搜索到了 就返回

for (int& it : edge[x]) {
if (dis[it] == INT_MAX) {
dis[it] = dis[x] + 1;
que.push(it);
}
}

### 没有搜索到 加入 下一层 的 que

}

return 0;
}
};

``````class Solution {
public:
unordered_map<string, int> wordId;
vector<vector<int>> edge;
int nodeNum = 0;

if (!wordId.count(word)) {
wordId[word] = nodeNum++;
edge.emplace_back();
}
}

int id1 = wordId[word];
for (char& it : word) {
char tmp = it;
it = '*';
int id2 = wordId[word];
edge[id1].push_back(id2);
edge[id2].push_back(id1);
it = tmp;
}
}

int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
for (string& word : wordList) {
}
if (!wordId.count(endWord)) {
return 0;
}

vector<int> disBegin(nodeNum, INT_MAX);
int beginId = wordId[beginWord];
disBegin[beginId] = 0;
queue<int> queBegin;
queBegin.push(beginId);

vector<int> disEnd(nodeNum, INT_MAX);
int endId = wordId[endWord];
disEnd[endId] = 0;
queue<int> queEnd;
queEnd.push(endId);

while (!queBegin.empty() && !queEnd.empty()) {
int queBeginSize = queBegin.size();
for (int i = 0; i < queBeginSize; ++i) {
int nodeBegin = queBegin.front();
queBegin.pop();
if (disEnd[nodeBegin] != INT_MAX) {
return (disBegin[nodeBegin] + disEnd[nodeBegin]) / 2 + 1;
}
for (int& it : edge[nodeBegin]) {
if (disBegin[it] == INT_MAX) {
disBegin[it] = disBegin[nodeBegin] + 1;
queBegin.push(it);
}
}
}

int queEndSize = queEnd.size();
for (int i = 0; i < queEndSize; ++i) {
int nodeEnd = queEnd.front();
queEnd.pop();
if (disBegin[nodeEnd] != INT_MAX) {
return (disBegin[nodeEnd] + disEnd[nodeEnd]) / 2 + 1;
}
for (int& it : edge[nodeEnd]) {
if (disEnd[it] == INT_MAX) {
disEnd[it] = disEnd[nodeEnd] + 1;
queEnd.push(it);
}
}
}
}
return 0;
}
};
``````

### 最后 答案 函数 不一样

int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
for (string& word : wordList) {
}
if (!wordId.count(endWord)) {
return 0;
}

vector<int> disBegin(nodeNum, INT_MAX);
int beginId = wordId[beginWord];
disBegin[beginId] = 0;
queue<int> queBegin;
queBegin.push(beginId);

vector<int> disEnd(nodeNum, INT_MAX);
int endId = wordId[endWord];
disEnd[endId] = 0;
queue<int> queEnd;
queEnd.push(endId);

### 两套，begin 和 end 两头同时 开始搜索

while (!queBegin.empty() && !queEnd.empty()) {
int queBeginSize = queBegin.size();
for (int i = 0; i < queBeginSize; ++i) {
int nodeBegin = queBegin.front();
queBegin.pop();
if (disEnd[nodeBegin] != INT_MAX) {

return (disBegin[nodeBegin] + disEnd[nodeBegin]) / 2 + 1;

}
for (int& it : edge[nodeBegin]) {
if (disBegin[it] == INT_MAX) {
disBegin[it] = disBegin[nodeBegin] + 1;
queBegin.push(it);

### 没有出现过的 节点 就 压进去。

}
}
}

### end 同理

int queEndSize = queEnd.size();
for (int i = 0; i < queEndSize; ++i) {
int nodeEnd = queEnd.front();
queEnd.pop();
if (disBegin[nodeEnd] != INT_MAX) {
return (disBegin[nodeEnd] + disEnd[nodeEnd]) / 2 + 1;
}
for (int& it : edge[nodeEnd]) {
if (disEnd[it] == INT_MAX) {
disEnd[it] = disEnd[nodeEnd] + 1;
queEnd.push(it);
}
}
}
}
return 0;
}