LeetCode刷题(致敬跨年)单词拆分
题目描述
代码
class Solution {
public:
bool wordBreak(string s, vector
if(s.empty()||wordDict.empty())
return false;
int n=s.size();
vector
dp[0]=true;
for(int i=0;i<=n;i++)
{
for(auto word:wordDict)
{
int ws = word.size();
if(i - ws >= 0) {
if (!s.compare(i-ws, ws, word)&&dp[i-ws]) {
dp[i] = true;
break;
}
}
}
}
//从开始到是否在这里面
return dp[n];
}
};
2019.12.31不优化了不优化了 这代码留作以后修改吧
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment