1 Star 0 Fork 0

xiangxiang/LeetCode-NOTES

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
solution.cpp 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
默然 提交于 7年前 . update all algorithms.
class Solution
{
public:
vector<string> result;
void printStack(stack<string> stk)
{
string output = "";
while(!stk.empty())
{
if(output == "")
output += stk.top();
else
output = output + " " + stk.top();
stk.pop();
}
result.push_back(output);
}
void check(vector<vector<int> > &v, int t, stack<string> stk, string s)
{
if(t == -1)
{
printStack(stk);
return ;
}
else
{
for(vector<string>::size_type st = 0; st < v[t].size(); st ++)
{
stk.push(s.substr(v[t][st]+1, t-v[t][st]));
check(v, v[t][st], stk, s);
stk.pop();
}
}
}
vector<vector<int> > buildv(string s, unordered_set<string> &dict)
{
vector<vector<int> > v(s.length());
for(string::size_type st1 = 0; st1 < s.length(); st1 ++)
{
for(string::size_type st2 = 0; st2 <= st1; st2 ++)
{
if(dict.find(s.substr(st2, st1-st2+1)) != dict.end())
{
if(st2 == 0)
v[st1].push_back(-1);
else if(!v[st2-1].empty())
v[st1].push_back(st2-1);
}
}
}
return v;
}
vector<string> wordBreak(string s, unordered_set<string> &dict)
{
vector<vector<int> > v = buildv(s, dict);
stack<string> stk;
check(v, v.size()-1, stk, s);
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiangxiang920/LeetCode-NOTES.git
git@gitee.com:xiangxiang920/LeetCode-NOTES.git
xiangxiang920
LeetCode-NOTES
LeetCode-NOTES
master

搜索帮助