[leetcode]Palindrome Partitioning
来源:互联网 发布:官方同花顺炒股软件 编辑:程序博客网 时间:2024/06/05 05:15
Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]
解题思路:用数组vvi[i][j]表示从第i个元素到第j个元素是回文串,然后再根据vvi[i][j]进行深度递归遍历收集合格的回文。
class Solution {public: //判断字符串s [b,e]是不是回文 bool isPalindrome(string &s, int b, int e){ while(b < e){ if(s[b] != s[e]) return false; b++; e--; } return true; } //s源串, 二维数组表示vvi[i, j]i到j是回文, b开始的行也可以指列,vs临时收集串,vvs最终输出的地方 void collectAll(string &s, vector<vector<bool> > &vvi, int b, vector<string> &vs, vector<vector<string> > &vvs){ int n = s.size(); if(b == n){ vvs.push_back(vs); return; } for(int i = b; i < n; i++){ if(vvi[b][i]){ //如果vvi[b][i]为真表示b-i为回文串,否则一直查找到一个回文串为止 string str(s, b, i - b + 1); vs.push_back(str); collectAll(s, vvi, i + 1, vs, vvs); //递归是第i+1行的第i+1列 vs.pop_back(); } } } vector<vector<string>> partition(string s) { size_t n = s.size(); vector<vector<string> >vvs; if(n == 0) return vvs; vector<vector<bool> > vvi(n, vector<bool>(n, false)); //动态规划 vvi[i, j]表示从i到j是回文串 for(int i = 0; i < n; i++){ for(int j = i; j < n; j++){ vvi[i][j] = isPalindrome(s, i, j); } } vector<string> vs; collectAll(s, vvi, 0, vs, vvs); return vvs; }};
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
- c/c++程序运行时的内存情况
- android安全机制简介
- PAT 1017. A除以B
- 黑马程序员————学习笔记(第五天)
- PHP实现一维数组转换成二维数组实例
- [leetcode]Palindrome Partitioning
- LCD 调试经验总结
- spring MVC配置详解
- Android 命名规范(总结一)
- 转:大部分人努力程度之低,根本轮不到拼天赋
- Leetcode之remove系列
- Ubuntu添加开机自动启动程序的方法
- 2013.6.19雷阿伦总决赛关键一投
- NYOJ16 矩形嵌套 【DP】