class Solution {
public:
int minCut(string s) {
const int n = s.size();
vector<int> f(n+1);
//bool p[n][n];
vector<vector<bool>> p(n, vector<bool>(n,false));
for (int i = 0; i <= n; i++)
{
f[i] = n - 1 - i;
}
for (int i = n - 1; i >= 0; i--)
{
for (int j = i; j < n; j++)
{
if (s[i] == s[j] && ((j - i < 2) || p[i + 1][j - 1]))
{
p[i][j] = true;
f[i] = min(f[i], f[j + 1] + 1);
}
}
}
return f[0];
}
};
【LeetCode】Palindrome Partitioning II
最新推荐文章于 2019-06-01 11:25:07 发布