【LeetCode】131.Palindrome Partitioning回文划分
来源:互联网 发布:安徽省大数据产业协会 编辑:程序博客网 时间:2024/05/18 01:35
题目:
理解:即将一个字符串划分成回文子串,穷举所有的可能。
分析:
由我的【LeetCode】 5.Longest Palindromic Substring最长回文子串问题 这篇文章分析可知,判断回文子串的问题可以转换为一个动态规划的问题,这里我使用了动态规划来找到所有的回文子串,将其记录到一个二维数组中,然后将这个数据转化成一个等价图,利用一个动态图搜索算法来找到从起始节点到终止节点的路径,每一个节点便是字符串的一个分割点,由此可以得到问题的解。
代码:
<span style="font-size:12px;">class Solution {public: vector<vector<string>> partition(string s) { int P[s.length()][s.length()]; for(int i=0;i<s.length();i++) for(int j=0;j<s.length();j++) P[i][j]=0; for(int i=0;i<s.length();i++) P[i][i]=1; for(int i=0;i<s.length()-1;i++) { if(s[i]==s[i+1]) P[i][i+1]=1; } for(int len=3;len<=s.length();len++) { for(int i=0;i<s.length()-len+1;i++) { int j=i+len-1; if(P[i+1][j-1]==1&&s[i]==s[j]) P[i][j]=1; } } vector < vector<string> > table[s.length()+1]; vector <string> tempPath; table[0].push_back(tempPath); for(int j=1;j<=s.length();j++) { for(int i=0;i<j;i++)//找到前置节点 { if(P[i][j-1]==1)//若i为其前置节点 { for(int k=0;k<table[i].size();k++) { vector<string> temp=table[i][k]; temp.push_back(s.substr(i,j-i)); table[j].push_back(temp); } } } } return table[s.length()]; }};</span>
0 0
- 【LeetCode】131.Palindrome Partitioning回文划分
- leetcode【Palindrome Partitioning 回文字符串划分】
- LeetCode------------palindrome-partitioning(回文)
- LeetCode 131. Palindrome Partitioning(回文分区)
- [LeetCode] Palindrome Partitioning 回文分割
- 回文子串划分 Palindrome Partitioning
- leetcode 131. Palindrome Partitioning回文子串+DFS
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- Leetcode 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode *** 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode-131.Palindrome Partitioning
- Systrace Walkthrough
- SpannableString与SpannableStringBuilder DecimalFormat 和 BigFormat
- lightoj 1319 - Monkey Tradition (中国剩余定理)
- Android新手入门2016(13)--阻塞对话框PopupWindow
- Linux内核源码分析方法
- 【LeetCode】131.Palindrome Partitioning回文划分
- youtube下载视频好工具插件
- Linux apache安装
- Only the original thread that created a view hierarchy can touch its views.
- poj3624 Charm Bracelet(01背包)
- 多线程——Java多线程实现的三种方式
- 多个控件跑马灯效果
- Red Hat 7.2 配置使用CentOS 7.2 Yum 源
- 原创:为iOS和OSX开发VPN的九九八十一难