您的位置:首页 > 其它

LeetCode: Word Ladder II

2013-05-22 13:36 387 查看
看了别人的答案,这题的关键在于数据结构的选择,一开始选择的multimap肯定MLE

class Solution {
public:
void getPath(vector<vector<string> > &ansSet, vector<string> &ans, int now, vector<vector<int> > &pa, vector<string> &que)
{
if (now == -1)
{
ansSet.push_back(vector<string>(0));
for (int i = ans.size() - 1; i >= 0; i--)
ansSet.back().push_back(ans[i]);
return;
}
for (int i = 0; i < pa[now].size(); i++)
{
ans.push_back(que[now]);
getPath(ansSet, ans, pa[now][i], pa, que);
ans.pop_back();
}

}
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> que;
vector<int> step;
vector<vector<int> > pa;
unordered_map<string, int> hash;
step.push_back(1);
que.push_back(start);
pa.push_back(vector<int>(0));
pa.back().push_back(-1);
int head = 0;
int bestStep = -1;
vector<vector<string> > ans;
while (head < que.size())
{
string now = que[head];
int nowStep = step[head];
if (bestStep > -1 && nowStep == bestStep) break;
for (int i = 0; i < now.size(); i++)
for (char ch = 'a'; ch <= 'z'; ch++)
{
string next = now;
if (next[i] == ch) continue;
next[i] = ch;
if (next == end)
{
bestStep = nowStep + 1;
vector<string> single(0);
single.push_back(end);
getPath(ans, single, head, pa, que);
} else if (dict.count(next))
{
if (hash[next] == 0)
{
que.push_back(next);
step.push_back(nowStep + 1);
pa.push_back(vector<int>(0));
pa.back().push_back(head);
hash[next] = pa.size() - 1;
} else if (step[hash[next]] == nowStep + 1)
{
pa[hash[next]].push_back(head);
}
}
}
head++;
}
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: