Palindrome Partitioning II
来源:互联网 发布:明教捏脸数据 编辑:程序博客网 时间:2024/06/06 09:50
题目: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 = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
思路:
本题使用动态规划,一开始使用dp[i][j]二维数组代表从i到j能否形成回文串,这里面有一些小技巧比如:如果dp[i][j]是真的话,那么dp[i+1][j-1]也是真之类的。
当然,首先,count[i]=min{ count[i],count[j+1]+1 }&&dp[i][j]==1 这个核心的条件。
在最后的代码里面,最终判断的时候就一个判断句。
代码:
class Solution {public://https://leetcode.com/problems/palindrome-partitioning-ii/ int minCut(string s) { //核心公式 // count[i]=从i到尾的最小剪切数 // count[i]=min{ count[j+1]+1 } + dp[i][j]==1 j=i....s.length() int len = s.size(); int count[len+1]; bool dp[len][len]; for(int i = 0; i <= len; i++){ count[i] = len-i; } for(int i = 0; i < len; i++){ for(int j = 0; j < len; j++){ dp[i][j] = false; } } for(int i=len-1;i>=0;i--){ for(int j=i;j<=len-1;j++){ if((j-i<2||dp[i+1][j-1]==1)&& s[i]==s[j]){ dp[i][j]=true; count[i]=min( count[i],count[j+1]+1 ); } } } return count[0]-1; }};
0 0
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Palindrome Partitioning II
- Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Palindrome Partitioning II
- LetCode:Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- 黑马程序员--IO(下)
- 【Android杂谈】安卓开发必需了解 -AIDL(一)
- js中innerHTML和outerHTML的相同与不同
- PHP Laravel学习实践(一)最出色的php框架,让代码成为艺术
- 关于标准分治、动态规划、贪婪选择三种策略的比较
- Palindrome Partitioning II
- Qt学习之路(31): 一个简易画板的实现(QWidget)
- vs2010利用属性表自动配置OpenCV(XP的32位系统,opencv版本是2.4.10)
- 从问题到解决方案到应用-android-ApiDemo入口源代码学习及应用
- NES 6502
- NoSQL是什么
- java 理解CAS
- Can't load IA 32-bit .dll on a AMD 64-bit platform
- Oracle dblink详解(转)