[leetcode] Palindrome Partitioning II
2013-08-22 23:59
381 查看
Given 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 =
Return
the palindrome partitioning
be produced using 1 cut.我发现leetcode里面申请静态数组的时候可以不用指定数组的大小一定为一个常数
可以在上一道题的基础上做,但是过不了大数据的,稍微改进了一下,可以过的大数据更多了:
我的动态规划很不完美了,其实主要失败在如何把判断回文的过程加在整个填写表格的过程中,看看别人的:
原来判断回文也是一种动态规划。
dp(i)(j)=true if s(i)==s(j) && dp(i+1)(j-1)
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
1since
the palindrome partitioning
["aa","b"]could
be produced using 1 cut.我发现leetcode里面申请静态数组的时候可以不用指定数组的大小一定为一个常数
class Solution { public: int minCut(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int min=INT_MAX; int length,partition1=INT_MAX,partition2=INT_MAX; if((length=s.size())==0) return 0; if(length==1) return 0; partition1=1+minCut(string(s.begin()+1,s.end())); if(partition1<min) min=partition1; for(int i=2 ; i<=length ; i++){ string inner(s.begin(),s.begin()+i); string rest(s.begin()+i,s.end()); if(JudgePalindrome(inner)){ partition2=1+minCut(rest); if(rest=="") partition2-=1; } if(partition2<min) min=partition2; } return min; } bool JudgePalindrome(string s){ int length=s.size(); for(int i=0 ; i<length/2 ; i++){ if(s[i]!=s[length-i-1]) return false; } return true; } };
可以在上一道题的基础上做,但是过不了大数据的,稍微改进了一下,可以过的大数据更多了:
class Solution { public: int minCut(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int minimum=INT_MAX; int length,partition1=INT_MAX,partition2=INT_MAX; if((length=s.size())==0) return 0; if(length==1) return 0; int *f=new int[length]; bool (*array)[10000]=new bool[length][10000]; //**********************************************主要时间耗费在这里 for(int i=0 ; i<length ; i++){ array[i][i]=true; for(int j=i-1 ; j>=0 ; j--){ if(JudgePalindrome(s,j,i)) array[i][j]=true; else array[i][j]=false; } } //********************************************** memset(f,0,sizeof(int)*length); for(int i=1 ; i<length ; i++){ minimum=INT_MAX; for(int j=i-1 ; j>=0 ; j--){ if(array[i][j] && j>=1 && f[j-1]+1<minimum) minimum=f[j-1]+1; } if(array[i][0]) minimum=0; f[i]=min(f[i-1]+1,minimum); } return f[length-1]; } bool JudgePalindrome(const string& s,int i,int j){ int length=j-i+1; for( ; i<j ; i++,j--){ if(s[i]!=s[j]) return false; } return true; } };
我的动态规划很不完美了,其实主要失败在如何把判断回文的过程加在整个填写表格的过程中,看看别人的:
class Solution { public: int minCut(string str){ int leng = str.size(); int *dp=new int[leng+1]; bool (*palin)[10000]=new bool[leng][10000]; for(int i = 0; i <= leng; i++) dp[i] = leng-i; for(int i = 0; i < leng; i++) for(int j = 0; j < leng; j++) palin[i][j] = false; for(int i = leng-1; i >= 0; i--){ for(int j = i; j < leng; j++){ if(str[i] == str[j] && (j-i<2 || palin[i+1][j-1])){//这里非常的巧妙!!! palin[i][j] = true; dp[i] = min(dp[i],dp[j+1]+1); } } } return dp[0]-1; } };
原来判断回文也是一种动态规划。
dp(i)(j)=true if s(i)==s(j) && dp(i+1)(j-1)
相关文章推荐
- LeetCode #132: Palindrome Partitioning II
- Leetcode:Palindrome Partitioning II
- Palindrome Partitioning I,II[leetcode] DFS以及DP的解法
- 【LeetCode】palindrome-partitioning i&ii
- leetcode Palindrome Partitioning I II
- LeetCode之Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- LeetCode: Palindrome Partitioning II [132]
- 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 132. Palindrome Partitioning II(回文切分)
- LeetCode: Palindrome Partitioning II
- Leetcode 132. Palindrome Partitioning II
- LeetCode 132 Palindrome Partitioning II (动态规划)
- [Leetcode] 132. Palindrome Partitioning II