test for code


class Solution {
public:
struct info
{
int id;
int step;
};
void gen(vector &vstr,vector *c, int id,vector> &res)
{
vector v;
vector> tres;
if(c[id].size()==0)
{
v.push_back(vstr[id]);
res.push_back(v);
return;
}
int i,j;
for(i=0;i> findLadders(string start, string end, unordered_set &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector> res;
vector v;
if(start==end) //start is end
{
v.push_back(start);
res.push_back(v);
return res;
}
unordered_map mp;
unordered_map::iterator iter_m;
unordered_set::iterator iter_s;
vector vstr;
dict.insert(start);
dict.insert(end);
int id=0,i,j,k;
for(iter_s=dict.begin();iter_s!=dict.end();++iter_s)
{
vstr.push_back(*iter_s);
mp.insert(make_pair(*iter_s,id));
id++;
}
int nStart, nEnd;
iter_m = mp.find(start);
nStart = iter_m->second;
iter_m = mp.find(end);
nEnd = iter_m->second;

int len = start.length();
int nTotal = mp.size();
vector *c = new vector [nTotal];
int *step = new int [nTotal];
for(i=0;i q;
q.push(temp);
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.step>=nMin) continue;
string str = vstr[cur.id];
for(i=0;isecond;
if(step[id]<=cur.step) continue; if(step[id]!=cur.step+1) { temp.id=id; temp.step=cur.step+1; q.push(temp); step[id]=cur.step+1; if(id==nEnd) nMin = cur.step+1; //find end } c[id].push_back(cur.id); } } } if(c[nEnd].size()==0) //no result { delete[]c; delete[]step; return res; } gen(vstr,c,nEnd,res); delete[]c; delete[]step; return res; } };

《test for code》有一个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注