LeetCode:61.分割回文串
目录
1.分割回文串
1.分割回文串
class Solution {vector<vector<bool>> dp;vector<vector<string>> ret;vector<string> path;int n;
public:vector<vector<string>> partition(string s) {n = s.size();dp = vector<vector<bool>>(n, vector<bool>(n));for(int i = n - 1; i >= 0; i--)for(int j = i; j < n; j++)if(s[i] == s[j])dp[i][j] = i + 1 < j ? dp[i + 1][j - 1] : true;dfs(s, 0);return ret;}void dfs(string s, int pos){if(pos == s.size()){ret.push_back(path);return;}for(int end = pos; end < n; end++){if(dp[pos][end] == true){path.push_back(s.substr(pos, end - pos + 1));dfs(s, end + 1);path.pop_back();}}}
};