leetcode:Palindrome Partitioning
来源:互联网 发布:php网站流量统计系统 编辑:程序博客网 时间:2024/06/05 01:58
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"] ]
Subscribe to see which companies asked this question
class Solution { private: void partitionHelper(string &s, int idx, vector<vector<bool>> &dp, vector<string> &curVtr, vector<vector<string>> &retVtr) { if (idx == s.size()) { retVtr.push_back(curVtr); return; } for (int len=1; len<=(int)s.size()-idx; len++) { if (dp[idx][idx+len-1]) { curVtr.push_back(s.substr(idx, len)); partitionHelper(s, idx+len, dp, curVtr, retVtr); curVtr.pop_back(); } } } public: vector<vector<string>> partition(string s) { vector<vector<string>> retVtr; vector<string> curVtr; vector<vector<bool>> dp(s.size(), vector<bool>(s.size())); for (int i=0; i<s.size(); i++) dp[i][i] = true; for (int i=1; i<s.size(); i++) if (s[i-1] == s[i]) dp[i-1][i] = true; for (int len=2; len<=s.size()-1; len++) { //这里等号要注意 for (int i=0; i<=(int)s.size()-1-len; i++) { if (s[i] == s[i+len] && dp[i+1][i+len-1]) dp[i][i+len] = true; } } partitionHelper(s, 0, dp, curVtr, retVtr); return retVtr; }};
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
- 基于HttpClient的HttpUtils(后台访问URL)
- 使用Mockplus的九大理由
- 这是一个传奇的算法(平方根倒数速算法)
- cesium js学习一加载三维模型
- 三、 Hive 逻辑算子及其生成(下)
- leetcode:Palindrome Partitioning
- FastJson构造json串空的问题
- Window环境中开发Android之adb连接手机问题解决
- linux中如何快速在某目录下打开终端 及vim多文件调试
- uboot CMD 命令体系
- hdu2571 命运--DP
- leetcode ZigZag Conversion 006
- 排序——希尔排序(C++)
- gcc/g++编译