LeetCode-Word Break-断词
https://oj.leetcode.com/problems/word-break/
水题。简单DP。
class Solution {
public:
int n,m;
bool wordBreak(string s, unordered_set<string> &dict) {
n=s.length();
vector <bool> dp(n+1,0);
dp[0]=true;
for (int i=1;i<=n;i++){
for (int j=0;j<i;j++){
string cur=s.substr(j,i-j);
if (dict.find(cur)!=dict.end()){
if (dp[j]){
dp[i]=true;
break;
}
}
}
}
return dp[n];
}
};
浙公网安备 33010602011771号