class Solution{
public:
bool dfs(string s, vector<string>& wordDict,string m,int i){
if(i>=s.size()&& m.size()==0){
return true;
}
m+=s[i];
for(string word:wordDict){
if(m==word){
if(dfs(s,wordDict,"",i+1)){
return true;
}
m+=s[i];
}
}
if(dfs(s,wordDict,m,i+1)){
return true;
}
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
int n=s.size();
string m="";
m+=s[0];
return dfs(s,wordDict,m,1);
}
};
这会导致堆栈溢出 如果我在第三行中仅使用其中一个条件,即(i>=s.size() 或 m.size()==0) 那么它工作正常,但如果两个条件都被写入则会导致。