【LeetCode笔记】Palindrome Partitioning
来源:互联网 发布:秦皇岛旅游攻略知乎 编辑:程序博客网 时间:2024/06/14 01:42
2 DP methods are used in this implementation:
1. DP method to find all palindromes in string s:
This one is talk thoroughly in before...
2. DP method to find all partitions in string s:
If sub-string s[i..j] is a palindrome, then all partitions for s[0..j] is all partitions for s[0..i-1] + palindromes[i..j]
Code with explanations:
vector<vector<string> > partition(string s){int n = s.size();//pal[i][j]: substring s[i..j] is a palindrome or notvector<vector<bool> > pal(n, vector<bool>(n, false)); //solution[i + 1]: all partitions for substring s[0..i]vector<vector<vector<string> > > solution(n + 1); //solution[0] is a empty matrix: partition for substrings[0..-1]solution[0].push_back(vector<string>(0)); for(int j = 0; j < n; ++j)for(int i = 0; i <= j; ++i)if(s[i] == s[j] && (j - i < 2 || pal[i + 1][j - 1])){pal[i][j] = true;//DP method: all partitions for s[0..j]: all partitions for s[0..i-1] + palindromes[i..j]for(int k = 0; k < solution[i].size(); ++k){ //solution[i]: all partitions for s[0..i-1]solution[i][k].push_back(s.substr(i, j - i + 1)); //add palindromes[i..j]solution[j + 1].push_back(solution[i][k]); //solution[j + 1]: all partitions for s[0..j]solution[i][k].pop_back(); //be sure to recover solution[i] for later use}}return solution[n];}
0 0
- 【LeetCode笔记】Palindrome Partitioning
- leetcode解题笔记-Palindrome Partitioning
- 【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
- 教你制作一个OS X 10.9 Mavericks 安装U盘
- 面向对象-多态
- Mysql数据库-子查询和连接
- 解决Ubuntu14.04下Chromium安装Flash播放器
- 用户、组以及相关文件说明
- 【LeetCode笔记】Palindrome Partitioning
- Java中System类(传智播客毕老师视频讲解)
- ubuntu12.04 不识别触摸板
- 在Java中>、>>、>>>三者的区别
- 分类文件
- Java面试问题总结
- Java中引用与对象
- Java实现各类排序
- Java 中对象的销毁