leetcode---palindrome-partitioning---dp, dfs
来源:互联网 发布:化为简化阶梯型矩阵 编辑:程序博客网 时间:2024/04/26 23:09
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”]
]
class Solution {public: vector< vector<bool> > ok; void dfs(vector< vector<string> > &ans, vector<string> tmp, int dep, int n, const string &s) { if(dep >= n) { ans.push_back(tmp); } for(int j=dep; j<n; j++) { if(ok[dep][j]) { tmp.push_back(s.substr(dep, j-dep+1)); dfs(ans, tmp, j+1, n, s); tmp.pop_back(); } } } vector<vector<string>> partition(string s) { int n = s.size(); ok = vector< vector<bool> >(n, vector<bool>(n, false)); for(int i=n-1; i>=0; i--) { for(int j=i; j<n; j++) { if(s[i] == s[j] && (i+1 >= j-1 || ok[i+1][j-1])) ok[i][j] = true; } } vector< vector<string> > ans; vector<string> tmp; dfs(ans, tmp, 0, n, s); return ans; }};
阅读全文
0 0
- leetcode---palindrome-partitioning---dp, dfs
- [LeetCode]--131. Palindrome Partitioning(backTracking && DFS && DP)
- leetcode dfs Palindrome Partitioning
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- Palindrome Partitioning I,II[leetcode] DFS以及DP的解法
- leetcode -- Palindrome Partitioning -- 重点 dfs
- LeetCode 131 Palindrome Partitioning (DFS)
- [leetcode][DP] Palindrome Partitioning II
- leetcode---palindrome-partitioning-ii---dp
- Palindrome Partitioning I,II(DFS,DP)
- 131 Palindrome Partitioning(dp+dfs)
- leetcode 132. Palindrome Partitioning II 回文子串 + DFS + DP + 这道题需要认真学习
- leetcode Palindrome Partitioning II DP 深搜
- [leetcode][回溯][DP] Palindrome Partitioning //TODO
- Leetcode 132 - Palindrome Partitioning II(dp)
- 【DFS】Palindrome Partitioning
- 比较大小
- NOIP OIer光荣退役记
- indexOf方法和ES6的includes方法
- 269. Alien Dictionary
- IDEA spring的xml配置文件加载不到properties属性问题
- leetcode---palindrome-partitioning---dp, dfs
- 指针形式的图像的保存与转换
- IDEA(jetbrain通用)优雅级使用教程
- Tomcat7+Redis存储Session
- Redis
- 5.9
- Stanford Online-统计学习-ISLR-Ch3-Linear Regression
- MyBatis的collection只能查询一条记录
- Spring中 abstract="true"