LeetCode OJ:Palindrome Partitioning II
来源:互联网 发布:淘宝卖家千牛工作台 编辑:程序博客网 时间:2024/06/06 08:26
Palindrome Partitioning II
Total Accepted: 3866 Total Submissions: 22882My SubmissionsGiven a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
算法思路:
1、看到题目首先想到的是递归,虽然也进行了一些剪枝,但依然TLE,此路不通
class Solution{public:int palindrome(string s,int l,int r){while(r&&l<s.length()&&s[l]==s[r]){l++;r--;}if(l<r)return 0;return r-l-1;}void dfs(string str,int k,int n,int &minC){if(k>str.length()-1){minC=minC>n?n:minC;return;}if(n>=minC)return;for(int i=str.length()-1;i>=k;i--){int c=palindrome(str,k,i);if(c){n++;dfs(str,i+1,n,minC);n--;}}}int minCut(string s){int minC=0x7ffffff;dfs(s,0,0,minC);return minC-1;}};2、解决这种求最小个数的问题,动态规划是最有效的方式,
设字符串数组长度为len,设dp[i,j]是i到j最小分割数,则有dp[i,len]=min{dp[i,j]+dp[j+1,len]};因为我们不需要求出任意i,j间最小的分割数,也为简单起见,用一维数组就可以了,设dp[j]保存的是从j到len的最小分割数,则对i<=j<len,如果i到j是回文串,有dp[i]=min{dp[i],dp[j+1]};
现在的问题就是判断i到j是否是回文串,如果每次都利用循环判断,难免效率太低,这里同样也涉及到一个动态规划,设flag[i][j]保存的是i到j是否是回文串,则有如果flag[i+1][j-1]为true,并且字符串s[i]==s[j],则有flag[i][j]=true,否则为false,这里得考虑到i到i为true,如果i与j相邻,且s[i]==s[j],flag[i][j]也为true。
class Solution{public:int minCut(string s){int len=s.length();vector<int> dp(len+1);for(int i=0;i<=len;i++)dp[i]=len-i;vector<vector<bool>> flag;flag.resize(len+1);for(int i=0;i<=len;i++){flag[i].assign(len+1,false);}for(int i=len-1;i>=0;i--){for(int j=i;j<len;j++){if(s[i]==s[j]&&(j-i<=1||flag[i+1][j-1])){flag[i][j]=true;dp[i]=min(dp[i],dp[j+1]+1);}}}return dp[0]-1;}};
3、上面设的dp保存的i到字符串末的最小分割数,现在反过来,dp[i]保存的是0到i的最小分割数,稍作处理即可,如果j到i是回文串,则有dp[i]=min{dp[i],dp[j-1]+1};,这里因为有j-1,避免数组越界,可以从1到len遍历,相应的dp和flag保存的是第i(1到len)位置,比字符串下标多1
class Solution{public:int minCut(string s){int len=s.length();vector<int> dp(len+1);for(int i=0;i<=len;i++)dp[i]=i;vector<vector<bool>> flag;flag.resize(len+1);for(int i=0;i<=len;i++){flag[i].assign(len+1,false);}for(int i=1;i<=len;i++){for(int j=1;j<=i;j++){if(s[i-1]==s[j-1]&&(i-j<=1||flag[j+1][i-1])){flag[j][i]=true;dp[i]=min(dp[i],dp[j-1]+1);}}}return dp[len]-1;}};
0 0
- LeetCode OJ:Palindrome Partitioning II
- LeetCode OJ - Palindrome Partitioning II
- LeetCode OJ Palindrome Partitioning II
- LeetCode OJ:Palindrome Partitioning
- LeetCode OJ Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- LeetCode - Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- 在java项目中配置web service服务
- Android中的ToggleButton
- 盗版XP成主要恶意攻击对象
- 2073 活动选择问题【贪心】
- 总结java方法(函数)传值和传引用的问题
- LeetCode OJ:Palindrome Partitioning II
- jQuery-强大的jQuery选择器 (详解)
- POJ2063 Investment
- (七)中缀表达式转换后缀表达式算法
- 计算机图形学入门学习——阴影
- 《UNIX环境高级编程》笔记--守护进程
- 第5天 合并两个排序链表
- 怎样在视频添加字幕? 很简单3步曲.
- Permutation Sequence 排列序列-第K个排列