LeetCode Palindrome Partitioning
来源:互联网 发布:php fpm nginx 编辑:程序博客网 时间:2024/06/14 06:10
链接: https://oj.leetcode.com/problems/palindrome-partitioning/
dp+dfs
求字符串s的所有分割,使得每一个子串都是回文字符串.
dp数组用来储存s[i]~s[j]是否为回文串.由于二维数组传递不方便,所以用dp[i]来计算 :dp[i][j] ->dp[i*n+j]
if s[j]==s[j+i]&& dp[j+1][j+i-1]==true ;->dp[j][j+i]=true;
dfs(i,...) :求s[i~s.length()]的所有符合条件的分割.
class Solution{public: void dfs(int i,string s,int n,bool dp[] ,vector<string> &vet,vector<vector<string> > &ans) { if(i==n) { ans.push_back(vet); return ; } for(int j=i; j<n; j++) { if(dp[i*n+j]) { vet.push_back(s.substr(i,j-i+1)); dfs(j+1,s,n,dp,vet,ans); vet.pop_back(); } } } vector<vector<string> >partition(string s) { int n=s.size(); bool *dp=new bool[n*n]; vector<vector<string> > ans; vector<string> vet; if(n==0) return ans; for(int i = 0; i < n*n; i ++) dp[i] = false; for(int i = 0; i < n; i++) { dp[i*n+i] = true; } for(int i = 1; i < n; i ++) { for(int j = 0; j+i < n; j ++) { if(s[j] == s[j+i] && (dp[(j+1)*n+j+i-1]||i==1)) dp[j*n+j+i] = true; } } dfs(0,s,n,dp,vet,ans); return ans; }};
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- 博客新家来了!!!
- 在 iOS 7、8 中使用模糊效果
- C++ int,char,string,CString类型转换
- 隐藏系统保留区
- String,StringBuffer与StringBuilder的区别?
- LeetCode Palindrome Partitioning
- 原码 补码
- Viewpager 选项卡的滑动动画效果
- wav文件格式分析
- 黑马程序员——面向对象18:内部类
- 【HDU】 5088 Revenge of Nim II 高斯消元
- 《数据结构》 实验2
- Linux邻居子系统的细节之confirm-OpenVPN server模式的MAC地址学习
- Javadoc的Html文件转chm