79人阅读 评论(0)
class Solution {
public:

vector<string> next(string &s,unordered_set<string> &dict)
{
vector<string>result;
string temp=s;
int len = temp.size();
// unordered_set<string>::iterator iter;
for(int i=0;i<len;i++)
{

for(char j='a';j<='z';j++)
{
if(temp[i]==j) continue;

swap(temp[i],j);
if(dict.count(temp)!=0&&set1.find(temp)==set1.end())
{
result.push_back(temp);
set1.insert(temp);
}
swap(temp[i],j);
}

}

return result;
}

// vector<string>path;
set<string>set1;

vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
vector<string>path1;
vector<vector<string>> result;
if(start.size()==1)
{path1.push_back(start);
path1.push_back(end);
result.push_back(path1);
return result;
}
//set<string>set1;

// end1=end;
set1.insert(start);
//  path.push_back(start);
path1.push_back(start);
dfs(start,end,dict,result,path1);
//for(int i=0;i<result.size;i++)

return result;
}

void dfs(string s,string end,unordered_set<string> &dict,vector<vector<string>> &result,vector<string>path1)
{
vector<string>next1;
vector<string>::iterator iter;
next1=next(s,dict);

for(iter=next1.begin();iter!=next1.end();iter++)
{
if(*iter==end)
{
path1.push_back(*iter);
if(!result.empty()&&path1.size()<result[0].size())
{
result.clear();
result.push_back(path1);
}
if(result.empty()||path1.size()<=result[0].size())
result.push_back(path1);

//result.push_back(path1);
set1.erase(end);

// for(iter=path1.begin();iter!=path1.end();iter++)
// set1.erase(*(iter));

path1.pop_back();
string temp1=path1.back();
set1.erase(temp1);
return;
}

path1.push_back(*iter);
dfs(*iter,end,dict,result,path1);

//string temp2=path1.back();
path1.pop_back();
//set1.erase(temp2);

}

}

};   有点问题

class Solution {
public:

vector<string> next(string &s,unordered_set<string> &dict)
{
vector<string>result;
string temp=s;
int len = temp.size();
// unordered_set<string>::iterator iter;
for(int i=0;i<len;i++)
{

for(char j='a';j<='z';j++)
{
if(temp[i]==j) continue;

swap(temp[i],j);
if(dict.count(temp)!=0&&set1.find(temp)==set1.end())
{
result.push_back(temp);
set1.insert(temp);
}
swap(temp[i],j);
}

}

return result;
}

// vector<string>path;
set<string>set1;

vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {

//set<string>set1;
vector<string>path1;
vector<vector<string>> result;
// end1=end;
set1.insert(start);
//  path.push_back(start);
path1.push_back(start);
dfs(start,end,dict,result,path1);
return result;
}

void dfs(string s,string end,unordered_set<string> &dict,vector<vector<string>> &result,vector<string>path1)
{
vector<string>next1;
vector<string>::iterator iter;
next1=next(s,dict);

for(iter=next1.begin();iter!=next1.end();iter++)
{
if(*iter==end)
{
path1.push_back(*iter);
result.push_back(path1);
set1.erase(end);
while(iter!=next1.end())
{
set1.erase(*(iter++));
}
for(iter=path1.begin();iter!=path1.end();iter++)
set1.erase(*(iter));
path1.pop_back();
return;
}

path1.push_back(*iter);
dfs(*iter,end,dict,result,path1);
path1.pop_back();

}

}
};

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：15414次
• 积分：1339
• 等级：
• 排名：千里之外
• 原创：120篇
• 转载：13篇
• 译文：0篇
• 评论：0条
阅读排行
评论排行